rust-clippy
rust-clippy copied to clipboard
Suggest `use ... as _` when importing traits only
What it does
When importing names that are not used directly, but used as a trait, I think it is better to use the as _ clause to avoid polluting current scope with unused names.
Advantage
- does not introduce unused names into the current scope
- fewer potential "got-chas"
Drawbacks
No response
Example
// the `Write` here is never actually used
use std::fmt::Write;
fn main() {
let mut s = String::new();
let _ = write!(s, "hello, world!");
println!("{s}");
}
Could be written as:
use std::fmt::Write as _;
...
Any plans to add this?
I guess, as with any other FOSS projects... volunteers needed :)
TBH, I don't have the slightest idea of how to evaluate this, but I guess unused_imports compiler warning does exactly the same steps: checks that an import is neither (a) used directly nor (b) used as a trait import. For this lint case, you would need to do the same, but only show a lint/suggestion if (a) is false, but (b) is true.