summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_resolve/src/check_unused.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_resolve/src/check_unused.rs')
-rw-r--r--compiler/rustc_resolve/src/check_unused.rs26
1 files changed, 11 insertions, 15 deletions
diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs
index 7dbbd4c34..3b1f957c8 100644
--- a/compiler/rustc_resolve/src/check_unused.rs
+++ b/compiler/rustc_resolve/src/check_unused.rs
@@ -59,7 +59,6 @@ struct UnusedImportCheckVisitor<'a, 'b, 'tcx> {
base_use_tree: Option<&'a ast::UseTree>,
base_id: ast::NodeId,
item_span: Span,
- base_use_is_pub: bool,
}
struct ExternCrateToLint {
@@ -146,7 +145,6 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
// because this means that they were generated in some fashion by the
// compiler and we don't need to consider them.
ast::ItemKind::Use(..) if item.span.is_dummy() => return,
- ast::ItemKind::Use(..) => self.base_use_is_pub = item.vis.kind.is_pub(),
ast::ItemKind::ExternCrate(orig_name) => {
self.extern_crate_items.push(ExternCrateToLint {
id: item.id,
@@ -173,7 +171,7 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
self.base_use_tree = Some(use_tree);
}
- if self.base_use_is_pub {
+ if self.r.effective_visibilities.is_exported(self.r.local_def_id(id)) {
self.check_import_as_underscore(use_tree, id);
return;
}
@@ -332,13 +330,12 @@ impl Resolver<'_, '_> {
base_use_tree: None,
base_id: ast::DUMMY_NODE_ID,
item_span: DUMMY_SP,
- base_use_is_pub: false,
};
visit::walk_crate(&mut visitor, krate);
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) {
+ let 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()));
@@ -356,20 +353,19 @@ impl Resolver<'_, '_> {
}
};
- let len = spans.len();
- spans.sort();
- let ms = MultiSpan::from_spans(spans.clone());
- let mut span_snippets = spans
+ let ms = MultiSpan::from_spans(spans);
+
+ let mut span_snippets = ms
+ .primary_spans()
.iter()
- .filter_map(|s| match tcx.sess.source_map().span_to_snippet(*s) {
- Ok(s) => Some(format!("`{s}`")),
- _ => None,
- })
+ .filter_map(|span| tcx.sess.source_map().span_to_snippet(*span).ok())
+ .map(|s| format!("`{s}`"))
.collect::<Vec<String>>();
span_snippets.sort();
+
let msg = format!(
"unused import{}{}",
- pluralize!(len),
+ pluralize!(ms.primary_spans().len()),
if !span_snippets.is_empty() {
format!(": {}", span_snippets.join(", "))
} else {
@@ -379,7 +375,7 @@ impl Resolver<'_, '_> {
let fix_msg = if fixes.len() == 1 && fixes[0].0 == unused.item_span {
"remove the whole `use` item"
- } else if spans.len() > 1 {
+ } else if ms.primary_spans().len() > 1 {
"remove the unused imports"
} else {
"remove the unused import"