Fix autofix for self and self as … in unused_imports lint

This fixes two problems with the autofixes for the `unused_imports`
lint:

- `use std::collections::{HashMap, self as coll};` would suggest, when
  `HashMap` is unused, the incorrect `use std::collections::self as coll;`
  which does not compile.

- `use std::borrow::{self, Cow};` would suggest, when `self` is unused,
  `use std::borrow::{Cow};`, which contains unnecessary brackets.
This commit is contained in:
Samuel Tardieu 2025-03-24 11:53:09 +01:00
parent ae8ab87de4
commit 856a181570
4 changed files with 105 additions and 1 deletions

View file

@ -337,7 +337,8 @@ fn calc_unused_spans(
}
}
contains_self |= use_tree.prefix == kw::SelfLower
&& matches!(use_tree.kind, ast::UseTreeKind::Simple(None));
&& matches!(use_tree.kind, ast::UseTreeKind::Simple(_))
&& !unused_import.unused.contains(&use_tree_id);
previous_unused = remove.is_some();
}
if unused_spans.is_empty() {