Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block lint redundant_import #123768

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 44 additions & 42 deletions compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::Resolver;
use crate::{LexicalScopeBinding, NameBindingKind};
use rustc_ast as ast;
use rustc_ast::visit::{self, Visitor};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_data_structures::unord::UnordSet;
use rustc_errors::{pluralize, MultiSpan};
use rustc_hir::def::{DefKind, Res};
Expand Down Expand Up @@ -494,44 +494,46 @@ impl Resolver<'_, '_> {
);
}

// FIXME(#121708): Block redundant_import report and wait for lang team's decision.
let unused_imports = visitor.unused_imports;
let mut check_redundant_imports = FxIndexSet::default();
for module in self.arenas.local_modules().iter() {
for (_key, resolution) in self.resolutions(*module).borrow().iter() {
let resolution = resolution.borrow();

if let Some(binding) = resolution.binding
&& let NameBindingKind::Import { import, .. } = binding.kind
&& let ImportKind::Single { id, .. } = import.kind
{
if let Some(unused_import) = unused_imports.get(&import.root_id)
&& unused_import.unused.contains(&id)
{
continue;
}

check_redundant_imports.insert(import);
}
}
}

let mut redundant_imports = UnordSet::default();
for import in check_redundant_imports {
if self.check_for_redundant_imports(import)
&& let Some(id) = import.id()
{
redundant_imports.insert(id);
}
}
// let mut check_redundant_imports = FxIndexSet::default();
// for module in self.arenas.local_modules().iter() {
// for (_key, resolution) in self.resolutions(*module).borrow().iter() {
// let resolution = resolution.borrow();

// if let Some(binding) = resolution.binding
// && let NameBindingKind::Import { import, .. } = binding.kind
// && let ImportKind::Single { id, .. } = import.kind
// {
// if let Some(unused_import) = unused_imports.get(&import.root_id)
// && unused_import.unused.contains(&id)
// {
// continue;
// }

// check_redundant_imports.insert(import);
// }
// }
// }

// let mut redundant_imports = UnordSet::default();
// for import in check_redundant_imports {
// if self.check_for_redundant_imports(import)
// && let Some(id) = import.id()
// {
// redundant_imports.insert(id);
// }
// }

// The lint fixes for unused_import and unnecessary_qualification may conflict.
// Deleting both unused imports and unnecessary segments of an item may result
// in the item not being found.
for unn_qua in &self.potentially_unnecessary_qualifications {
if let LexicalScopeBinding::Item(name_binding) = unn_qua.binding
&& let NameBindingKind::Import { import, .. } = name_binding.kind
&& (is_unused_import(import, &unused_imports)
|| is_redundant_import(import, &redundant_imports))
&& is_unused_import(import, &unused_imports)
// && (is_unused_import(import, &unused_imports)
// || is_redundant_import(import, &redundant_imports))
{
continue;
}
Expand All @@ -545,17 +547,17 @@ impl Resolver<'_, '_> {
);
}

fn is_redundant_import(
import: Import<'_>,
redundant_imports: &UnordSet<ast::NodeId>,
) -> bool {
if let Some(id) = import.id()
&& redundant_imports.contains(&id)
{
return true;
}
false
}
// fn is_redundant_import(
// import: Import<'_>,
// redundant_imports: &UnordSet<ast::NodeId>,
// ) -> bool {
// if let Some(id) = import.id()
// && redundant_imports.contains(&id)
// {
// return true;
// }
// false
// }

fn is_unused_import(
import: Import<'_>,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
None
}

#[allow(dead_code)]
pub(crate) fn check_for_redundant_imports(&mut self, import: Import<'a>) -> bool {
// This function is only called for single imports.
let ImportKind::Single {
Expand Down
1 change: 1 addition & 0 deletions tests/ui/imports/redundant-import-issue-121915-2015.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
//@ compile-flags: --extern aux_issue_121915 --edition 2015
//@ aux-build: aux-issue-121915.rs

Expand Down
1 change: 1 addition & 0 deletions tests/ui/imports/redundant-import-issue-121915.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
//@ compile-flags: --extern aux_issue_121915 --edition 2018
//@ aux-build: aux-issue-121915.rs

Expand Down
1 change: 1 addition & 0 deletions tests/ui/imports/suggest-remove-issue-121315.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
//@ compile-flags: --edition 2021
#![deny(unused_imports)]
#![allow(dead_code)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
//@ run-rustfix
//@ edition:2021
#![deny(unused_qualifications)]
Expand Down
1 change: 1 addition & 0 deletions tests/ui/lint/unused/issue-59896.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
#![deny(unused_imports)]

struct S;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/lint/use-redundant/use-redundant-glob-parent.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
//@ check-pass
#![warn(unused_imports)]

Expand Down
1 change: 1 addition & 0 deletions tests/ui/lint/use-redundant/use-redundant-glob.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
//@ check-pass
#![warn(unused_imports)]

Expand Down
1 change: 1 addition & 0 deletions tests/ui/lint/use-redundant/use-redundant-issue-71450.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
//@ check-pass

#![warn(unused_imports)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
//@ check-pass
#![warn(unused_imports)]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
//@ check-pass
//@ edition:2021
#![warn(unused_imports)]
Expand Down
1 change: 1 addition & 0 deletions tests/ui/lint/use-redundant/use-redundant.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ ignore-test
//@ check-pass
#![warn(unused_imports)]

Expand Down
Loading