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 _ if the type is used for traits calls only #10606

Closed
nyurik opened this issue Apr 7, 2023 · 1 comment
Closed

Suggest use ... as _ if the type is used for traits calls only #10606

nyurik opened this issue Apr 7, 2023 · 1 comment
Labels
A-lint Area: New lints

Comments

@nyurik
Copy link
Contributor

nyurik commented Apr 7, 2023

What it does

If a specific trait is brought into the scope just because it extends some type, but not used directly, alias it as an anonymous _. I have frequently seen it with the common for std::io::Write:

use std::io::Write as _;  // <-- don't introduce the "Write" trait into the scope

fn main() {
    std::io::stdout().write_all(b"Hello, world!").unwrap();
}

Lint Name

use_non_polluting_alias_for_traits

Category

No response

Advantage

  • does not introduce (directly) unused traits into the scope

Drawbacks

No response

Example

For trait methods like

mod ext {
    pub trait Foo {
        fn foo(&self) {
            println!("foo");
        }
    }

    impl Foo for i32 {}
}

This example

fn main() {
    use crate::ext::Foo;  // <- no aliasing, polluting
    42.foo();
}

Could be written as:

fn main() {
    use crate::ext::Foo as _; // non-polluting aliasing
    42.foo();
}
@nyurik nyurik added the A-lint Area: New lints label Apr 7, 2023
@Alexendoo
Copy link
Member

Duplicate of #9021

@Alexendoo Alexendoo marked this as a duplicate of #9021 Apr 7, 2023
@Alexendoo Alexendoo closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2023
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

No branches or pull requests

2 participants