From 5681b91fa66213677390820f45339b81c2f4eead Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Sat, 8 Dec 2018 12:45:13 +0100 Subject: [PATCH] unused_imports: make the lint machine-applicable --- src/librustc/lint/builtin.rs | 10 ++ src/librustc_resolve/check_unused.rs | 203 ++++++++++++++++++++++++--- 2 files changed, 191 insertions(+), 22 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 6ae7448645a20..a659f7e9b3440 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -467,6 +467,7 @@ pub enum BuiltinLintDiagnostics { MacroExpandedMacroExportsAccessedByAbsolutePaths(Span), ElidedLifetimesInPaths(usize, Span, bool, Span, String), UnknownCrateTypes(Span, String, String), + UnusedImports(String, Vec<(Span, String)>), } impl BuiltinLintDiagnostics { @@ -548,6 +549,15 @@ impl BuiltinLintDiagnostics { BuiltinLintDiagnostics::UnknownCrateTypes(span, note, sugg) => { db.span_suggestion(span, ¬e, sugg, Applicability::MaybeIncorrect); } + BuiltinLintDiagnostics::UnusedImports(message, replaces) => { + if !replaces.is_empty() { + db.multipart_suggestion( + &message, + replaces, + Applicability::MachineApplicable, + ); + } + } } } } diff --git a/src/librustc_resolve/check_unused.rs b/src/librustc_resolve/check_unused.rs index 639960827c996..3b6179f78558b 100644 --- a/src/librustc_resolve/check_unused.rs +++ b/src/librustc_resolve/check_unused.rs @@ -7,23 +7,52 @@ // // Unused trait imports can't be checked until the method resolution. We save // candidates here, and do the actual check in librustc_typeck/check_unused.rs. +// +// Checking for unused imports is split into three steps: +// +// - `UnusedImportCheckVisitor` walks the AST to find all the unused imports +// inside of `UseTree`s, recording their `NodeId`s and grouping them by +// the parent `use` item +// +// - `calc_unused_spans` then walks over all the `use` items marked in the +// previous step to collect the spans associated with the `NodeId`s and to +// calculate the spans that can be removed by rustfix; This is done in a +// separate step to be able to collapse the adjacent spans that rustfix +// will remove +// +// - `check_crate` finally emits the diagnostics based on the data generated +// in the last step use std::ops::{Deref, DerefMut}; use crate::Resolver; use crate::resolve_imports::ImportDirectiveSubclass; -use rustc::{lint, ty}; use rustc::util::nodemap::NodeMap; +use rustc::{lint, ty}; +use rustc_data_structures::fx::FxHashSet; use syntax::ast; use syntax::visit::{self, Visitor}; use syntax_pos::{Span, MultiSpan, DUMMY_SP}; +struct UnusedImport<'a> { + use_tree: &'a ast::UseTree, + use_tree_id: ast::NodeId, + item_span: Span, + unused: FxHashSet, +} + +impl<'a> UnusedImport<'a> { + fn add(&mut self, id: ast::NodeId) { + self.unused.insert(id); + } +} struct UnusedImportCheckVisitor<'a, 'b: 'a> { resolver: &'a mut Resolver<'b>, /// All the (so far) unused imports, grouped path list - unused_imports: NodeMap>, + unused_imports: NodeMap>, + base_use_tree: Option<&'a ast::UseTree>, base_id: ast::NodeId, item_span: Span, } @@ -46,7 +75,7 @@ impl<'a, 'b> DerefMut for UnusedImportCheckVisitor<'a, 'b> { impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> { // We have information about whether `use` (import) directives are actually // used now. If an import is not used at all, we signal a lint error. - fn check_import(&mut self, item_id: ast::NodeId, id: ast::NodeId, span: Span) { + fn check_import(&mut self, id: ast::NodeId) { let mut used = false; self.per_ns(|this, ns| used |= this.used_imports.contains(&(id, ns))); if !used { @@ -54,16 +83,31 @@ impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> { // Check later. return; } - self.unused_imports.entry(item_id).or_default().insert(id, span); + self.unused_import(self.base_id).add(id); } else { // This trait import is definitely used, in a way other than // method resolution. self.maybe_unused_trait_imports.remove(&id); - if let Some(i) = self.unused_imports.get_mut(&item_id) { - i.remove(&id); + if let Some(i) = self.unused_imports.get_mut(&self.base_id) { + i.unused.remove(&id); } } } + + fn unused_import(&mut self, id: ast::NodeId) -> &mut UnusedImport<'a> { + let use_tree_id = self.base_id; + let use_tree = self.base_use_tree.unwrap(); + let item_span = self.item_span; + + self.unused_imports + .entry(id) + .or_insert_with(|| UnusedImport { + use_tree, + use_tree_id, + item_span, + unused: FxHashSet::default(), + }) + } } impl<'a, 'b> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b> { @@ -88,31 +132,112 @@ impl<'a, 'b> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b> { // This allows the grouping of all the lints in the same item if !nested { self.base_id = id; + self.base_use_tree = Some(use_tree); } if let ast::UseTreeKind::Nested(ref items) = use_tree.kind { - // If it's the parent group, cover the entire use item - let span = if nested { - use_tree.span - } else { - self.item_span - }; - if items.is_empty() { - self.unused_imports - .entry(self.base_id) - .or_default() - .insert(id, span); + self.unused_import(self.base_id).add(id); } } else { - let base_id = self.base_id; - self.check_import(base_id, id, use_tree.span); + self.check_import(id); } visit::walk_use_tree(self, use_tree, id); } } +enum UnusedSpanResult { + Used, + FlatUnused(Span, Span), + NestedFullUnused(Vec, Span), + NestedPartialUnused(Vec, Vec), +} + +fn calc_unused_spans( + unused_import: &UnusedImport<'_>, + use_tree: &ast::UseTree, + use_tree_id: ast::NodeId, +) -> UnusedSpanResult { + // The full span is the whole item's span if this current tree is not nested inside another + // This tells rustfix to remove the whole item if all the imports are unused + let full_span = if unused_import.use_tree.span == use_tree.span { + unused_import.item_span + } else { + use_tree.span + }; + match use_tree.kind { + ast::UseTreeKind::Simple(..) | ast::UseTreeKind::Glob => { + if unused_import.unused.contains(&use_tree_id) { + UnusedSpanResult::FlatUnused(use_tree.span, full_span) + } else { + UnusedSpanResult::Used + } + } + ast::UseTreeKind::Nested(ref nested) => { + if nested.len() == 0 { + return UnusedSpanResult::FlatUnused(use_tree.span, full_span); + } + + let mut unused_spans = Vec::new(); + let mut to_remove = Vec::new(); + let mut all_nested_unused = true; + let mut previous_unused = false; + for (pos, (use_tree, use_tree_id)) in nested.iter().enumerate() { + let remove = match calc_unused_spans(unused_import, use_tree, *use_tree_id) { + UnusedSpanResult::Used => { + all_nested_unused = false; + None + } + UnusedSpanResult::FlatUnused(span, remove) => { + unused_spans.push(span); + Some(remove) + } + UnusedSpanResult::NestedFullUnused(mut spans, remove) => { + unused_spans.append(&mut spans); + Some(remove) + } + UnusedSpanResult::NestedPartialUnused(mut spans, mut to_remove_extra) => { + all_nested_unused = false; + unused_spans.append(&mut spans); + to_remove.append(&mut to_remove_extra); + None + } + }; + if let Some(remove) = remove { + let remove_span = if nested.len() == 1 { + remove + } else if pos == nested.len() - 1 || !all_nested_unused { + // Delete everything from the end of the last import, to delete the + // previous comma + nested[pos - 1].0.span.shrink_to_hi().to(use_tree.span) + } else { + // Delete everything until the next import, to delete the trailing commas + use_tree.span.to(nested[pos + 1].0.span.shrink_to_lo()) + }; + + // Try to collapse adjacent spans into a single one. This prevents all cases of + // overlapping removals, which are not supported by rustfix + if previous_unused && !to_remove.is_empty() { + let previous = to_remove.pop().unwrap(); + to_remove.push(previous.to(remove_span)); + } else { + to_remove.push(remove_span); + } + } + previous_unused = remove.is_some(); + } + if unused_spans.is_empty() { + UnusedSpanResult::Used + } else if all_nested_unused { + UnusedSpanResult::NestedFullUnused(unused_spans, full_span) + } else { + UnusedSpanResult::NestedPartialUnused(unused_spans, to_remove) + } + } + } +} + pub fn check_crate(resolver: &mut Resolver<'_>, krate: &ast::Crate) { for directive in resolver.potentially_unused_imports.iter() { match directive.subclass { @@ -152,14 +277,33 @@ pub fn check_crate(resolver: &mut Resolver<'_>, krate: &ast::Crate) { let mut visitor = UnusedImportCheckVisitor { resolver, unused_imports: Default::default(), + base_use_tree: None, base_id: ast::DUMMY_NODE_ID, item_span: DUMMY_SP, }; visit::walk_crate(&mut visitor, krate); - for (id, spans) in &visitor.unused_imports { + for unused in visitor.unused_imports.values() { + let mut fixes = Vec::new(); + let mut spans = match calc_unused_spans(unused, unused.use_tree, unused.use_tree_id) { + UnusedSpanResult::Used => continue, + UnusedSpanResult::FlatUnused(span, remove) => { + fixes.push((remove, String::new())); + vec![span] + } + UnusedSpanResult::NestedFullUnused(spans, remove) => { + fixes.push((remove, String::new())); + spans + } + UnusedSpanResult::NestedPartialUnused(spans, remove) => { + for fix in &remove { + fixes.push((*fix, String::new())); + } + spans + } + }; + let len = spans.len(); - let mut spans = spans.values().cloned().collect::>(); spans.sort(); let ms = MultiSpan::from_spans(spans.clone()); let mut span_snippets = spans.iter() @@ -177,6 +321,21 @@ pub fn check_crate(resolver: &mut Resolver<'_>, krate: &ast::Crate) { } else { String::new() }); - visitor.session.buffer_lint(lint::builtin::UNUSED_IMPORTS, *id, ms, &msg); + + let fix_msg = if fixes.len() == 1 && fixes[0].0 == unused.item_span { + "remove the whole `use` item" + } else if spans.len() > 1 { + "remove the unused imports" + } else { + "remove the unused import" + }; + + visitor.session.buffer_lint_with_diagnostic( + lint::builtin::UNUSED_IMPORTS, + unused.use_tree_id, + ms, + &msg, + lint::builtin::BuiltinLintDiagnostics::UnusedImports(fix_msg.into(), fixes), + ); } }