Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest use ... as _ when importing traits only #11969

Closed
nyurik opened this issue Dec 16, 2023 · 2 comments · Fixed by #13322
Closed

Suggest use ... as _ when importing traits only #11969

nyurik opened this issue Dec 16, 2023 · 2 comments · Fixed by #13322
Labels
A-lint Area: New lints

Comments

@nyurik
Copy link
Contributor

nyurik commented Dec 16, 2023

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

...
@johnsonw
Copy link

Any plans to add this?

@nyurik
Copy link
Contributor Author

nyurik commented Aug 25, 2024

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.

@y21 y21 linked a pull request Sep 2, 2024 that will close this issue
bors added a commit that referenced this issue Sep 22, 2024
Unused trait imports (formerly anonymous trait import)

For #11969

I'm looking for help and feedback on implementing a new lint for suggesting `use ... as _` for traits where possible.

I have had a go at implementing this but I don't know if this is the best way to do it as I am new to clippy.

There are some edge cases I can think of where this doesn't work but have aired on the side of false negatives instead of false positives.

An example of a false negative. I couldn't figure out the best way to resolve an import from within clippy. The sub module imports MyAny so that cannot be anonymized but `use std::any::Any` could be. In this case it is not caught because `Any` and `MyAny` have the same DefId.
```rust
mod nested_mod_used_bad1 {
    use std::any::Any;
    use std::any::Any as MyAny;
    mod foo {
        use crate::nested_mod_used_bad1::MyAny;
        fn foo() {
            println!("{:?}", MyAny::type_id("foo"));
        }
    }
}
```

Any feedback is much appreciated.
bors added a commit that referenced this issue Sep 22, 2024
Unused trait imports (formerly anonymous trait import)

For #11969

I'm looking for help and feedback on implementing a new lint for suggesting `use ... as _` for traits where possible.

I have had a go at implementing this but I don't know if this is the best way to do it as I am new to clippy.

There are some edge cases I can think of where this doesn't work but have aired on the side of false negatives instead of false positives.

An example of a false negative. I couldn't figure out the best way to resolve an import from within clippy. The sub module imports MyAny so that cannot be anonymized but `use std::any::Any` could be. In this case it is not caught because `Any` and `MyAny` have the same DefId.
```rust
mod nested_mod_used_bad1 {
    use std::any::Any;
    use std::any::Any as MyAny;
    mod foo {
        use crate::nested_mod_used_bad1::MyAny;
        fn foo() {
            println!("{:?}", MyAny::type_id("foo"));
        }
    }
}
```

Any feedback is much appreciated.

-------
changelog: new lint: `unused_trait_names`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants