|
23 | 23 | // - `check_unused` finally emits the diagnostics based on the data generated
|
24 | 24 | // in the last step
|
25 | 25 |
|
26 |
| -use crate::imports::ImportKind; |
| 26 | +use crate::imports::{Import, ImportKind}; |
27 | 27 | use crate::module_to_string;
|
28 | 28 | use crate::Resolver;
|
29 | 29 |
|
30 |
| -use crate::NameBindingKind; |
| 30 | +use crate::{LexicalScopeBinding, NameBindingKind}; |
31 | 31 | use rustc_ast as ast;
|
32 | 32 | use rustc_ast::visit::{self, Visitor};
|
33 | 33 | use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
|
34 | 34 | use rustc_data_structures::unord::UnordSet;
|
35 | 35 | use rustc_errors::{pluralize, MultiSpan};
|
36 | 36 | use rustc_hir::def::{DefKind, Res};
|
37 |
| -use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS}; |
| 37 | +use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES}; |
| 38 | +use rustc_session::lint::builtin::{UNUSED_IMPORTS, UNUSED_QUALIFICATIONS}; |
38 | 39 | use rustc_session::lint::BuiltinLintDiag;
|
39 | 40 | use rustc_span::symbol::{kw, Ident};
|
40 | 41 | use rustc_span::{Span, DUMMY_SP};
|
@@ -514,8 +515,59 @@ impl Resolver<'_, '_> {
|
514 | 515 | }
|
515 | 516 | }
|
516 | 517 |
|
| 518 | + let mut redundant_imports = UnordSet::default(); |
517 | 519 | for import in check_redundant_imports {
|
518 |
| - self.check_for_redundant_imports(import); |
| 520 | + if self.check_for_redundant_imports(import) |
| 521 | + && let Some(id) = import.id() |
| 522 | + { |
| 523 | + redundant_imports.insert(id); |
| 524 | + } |
| 525 | + } |
| 526 | + |
| 527 | + // The lint fixes for unused_import and unnecessary_qualification may conflict. |
| 528 | + // Deleting both unused imports and unnecessary segments of an item may result |
| 529 | + // in the item not being found. |
| 530 | + for unn_qua in &self.potentially_unnecessary_qualifications { |
| 531 | + if let LexicalScopeBinding::Item(name_binding) = unn_qua.binding |
| 532 | + && let NameBindingKind::Import { import, .. } = name_binding.kind |
| 533 | + && (is_unused_import(import, &unused_imports) |
| 534 | + || is_redundant_import(import, &redundant_imports)) |
| 535 | + { |
| 536 | + continue; |
| 537 | + } |
| 538 | + |
| 539 | + self.lint_buffer.buffer_lint_with_diagnostic( |
| 540 | + UNUSED_QUALIFICATIONS, |
| 541 | + unn_qua.node_id, |
| 542 | + unn_qua.path_span, |
| 543 | + "unnecessary qualification", |
| 544 | + BuiltinLintDiag::UnusedQualifications { removal_span: unn_qua.removal_span }, |
| 545 | + ); |
| 546 | + } |
| 547 | + |
| 548 | + fn is_redundant_import( |
| 549 | + import: Import<'_>, |
| 550 | + redundant_imports: &UnordSet<ast::NodeId>, |
| 551 | + ) -> bool { |
| 552 | + if let Some(id) = import.id() |
| 553 | + && redundant_imports.contains(&id) |
| 554 | + { |
| 555 | + return true; |
| 556 | + } |
| 557 | + false |
| 558 | + } |
| 559 | + |
| 560 | + fn is_unused_import( |
| 561 | + import: Import<'_>, |
| 562 | + unused_imports: &FxIndexMap<ast::NodeId, UnusedImport>, |
| 563 | + ) -> bool { |
| 564 | + if let Some(unused_import) = unused_imports.get(&import.root_id) |
| 565 | + && let Some(id) = import.id() |
| 566 | + && unused_import.unused.contains(&id) |
| 567 | + { |
| 568 | + return true; |
| 569 | + } |
| 570 | + false |
519 | 571 | }
|
520 | 572 | }
|
521 | 573 | }
|
0 commit comments