Skip to content

Commit

Permalink
Unrolled build for rust-lang#116905
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#116905 - Fenex:refactor/compiler/resolve, r=petrochenkov

refactor(compiler/resolve): simplify some code

Removes unnecessary allocate and double-sorting the same vector, makes the code a little nicer.
  • Loading branch information
rust-timer authored Oct 26, 2023
2 parents 698db85 + e68edb8 commit 95bc5aa
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl Resolver<'_, '_> {

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()));
Expand All @@ -353,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 {
Expand All @@ -376,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"
Expand Down

0 comments on commit 95bc5aa

Please sign in to comment.