|
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::{ |
| 38 | + MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS, UNUSED_QUALIFICATIONS, |
| 39 | +}; |
38 | 40 | use rustc_session::lint::BuiltinLintDiag; |
39 | 41 | use rustc_span::symbol::{kw, Ident}; |
40 | 42 | use rustc_span::{Span, DUMMY_SP}; |
@@ -514,8 +516,59 @@ impl Resolver<'_, '_> { |
514 | 516 | } |
515 | 517 | } |
516 | 518 |
|
| 519 | + let mut redundant_imports = FxIndexSet::default(); |
517 | 520 | for import in check_redundant_imports { |
518 | | - self.check_for_redundant_imports(import); |
| 521 | + if self.check_for_redundant_imports(import) |
| 522 | + && let Some(id) = import.id() |
| 523 | + { |
| 524 | + redundant_imports.insert(id); |
| 525 | + } |
| 526 | + } |
| 527 | + |
| 528 | + // The lint fixes for unused_import and unnecessary_qualification may conflict. |
| 529 | + // Deleting both unused imports and unnecessary segments of an item may result |
| 530 | + // in the item not being found. |
| 531 | + for unn_qua in &self.potentially_unnecessary_qualifications { |
| 532 | + if let LexicalScopeBinding::Item(name_binding) = unn_qua.binding |
| 533 | + && let NameBindingKind::Import { import, .. } = name_binding.kind |
| 534 | + && (check_if_unused_import(import, &unused_imports) |
| 535 | + || check_if_redundant_import(import, &redundant_imports)) |
| 536 | + { |
| 537 | + continue; |
| 538 | + } |
| 539 | + |
| 540 | + self.lint_buffer.buffer_lint_with_diagnostic( |
| 541 | + UNUSED_QUALIFICATIONS, |
| 542 | + unn_qua.node_id, |
| 543 | + unn_qua.path_span, |
| 544 | + "unnecessary qualification", |
| 545 | + BuiltinLintDiag::UnusedQualifications { removal_span: unn_qua.removal_span }, |
| 546 | + ); |
| 547 | + } |
| 548 | + |
| 549 | + fn check_if_redundant_import( |
| 550 | + import: Import<'_>, |
| 551 | + redundant_imports: &FxIndexSet<ast::NodeId>, |
| 552 | + ) -> bool { |
| 553 | + if let Some(id) = import.id() |
| 554 | + && redundant_imports.contains(&id) |
| 555 | + { |
| 556 | + return true; |
| 557 | + } |
| 558 | + false |
| 559 | + } |
| 560 | + |
| 561 | + fn check_if_unused_import( |
| 562 | + import: Import<'_>, |
| 563 | + unused_imports: &FxIndexMap<ast::NodeId, UnusedImport>, |
| 564 | + ) -> bool { |
| 565 | + if let Some(unused_import) = unused_imports.get(&import.root_id) |
| 566 | + && let Some(id) = import.id() |
| 567 | + && unused_import.unused.contains(&id) |
| 568 | + { |
| 569 | + return true; |
| 570 | + } |
| 571 | + false |
519 | 572 | } |
520 | 573 | } |
521 | 574 | } |
0 commit comments