rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Suggest `use ... as _` when importing traits only

Open nyurik opened this issue 1 year ago • 2 comments

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 _;

...

nyurik avatar Dec 16 '23 02:12 nyurik

Any plans to add this?

johnsonw avatar Aug 24 '24 21:08 johnsonw

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.

nyurik avatar Aug 25 '24 02:08 nyurik