Rollup merge of #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.
This commit is contained in:
Matthias Krüger 2023-10-26 17:45:43 +02:00 committed by GitHub
commit 17fb2f4b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -335,7 +335,7 @@ pub(crate) fn check_unused(&mut self, krate: &ast::Crate) {
for unused in visitor.unused_imports.values() { for unused in visitor.unused_imports.values() {
let mut fixes = Vec::new(); 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::Used => continue,
UnusedSpanResult::FlatUnused(span, remove) => { UnusedSpanResult::FlatUnused(span, remove) => {
fixes.push((remove, String::new())); fixes.push((remove, String::new()));
@ -353,20 +353,19 @@ pub(crate) fn check_unused(&mut self, krate: &ast::Crate) {
} }
}; };
let len = spans.len(); let ms = MultiSpan::from_spans(spans);
spans.sort();
let ms = MultiSpan::from_spans(spans.clone()); let mut span_snippets = ms
let mut span_snippets = spans .primary_spans()
.iter() .iter()
.filter_map(|s| match tcx.sess.source_map().span_to_snippet(*s) { .filter_map(|span| tcx.sess.source_map().span_to_snippet(*span).ok())
Ok(s) => Some(format!("`{s}`")), .map(|s| format!("`{s}`"))
_ => None,
})
.collect::<Vec<String>>(); .collect::<Vec<String>>();
span_snippets.sort(); span_snippets.sort();
let msg = format!( let msg = format!(
"unused import{}{}", "unused import{}{}",
pluralize!(len), pluralize!(ms.primary_spans().len()),
if !span_snippets.is_empty() { if !span_snippets.is_empty() {
format!(": {}", span_snippets.join(", ")) format!(": {}", span_snippets.join(", "))
} else { } else {
@ -376,7 +375,7 @@ pub(crate) fn check_unused(&mut self, krate: &ast::Crate) {
let fix_msg = if fixes.len() == 1 && fixes[0].0 == unused.item_span { let fix_msg = if fixes.len() == 1 && fixes[0].0 == unused.item_span {
"remove the whole `use` item" "remove the whole `use` item"
} else if spans.len() > 1 { } else if ms.primary_spans().len() > 1 {
"remove the unused imports" "remove the unused imports"
} else { } else {
"remove the unused import" "remove the unused import"