Skip to content

FP unused_trait_names #14924

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

Open
matthiaskrgr opened this issue May 30, 2025 · 2 comments · May be fixed by #14947
Open

FP unused_trait_names #14924

matthiaskrgr opened this issue May 30, 2025 · 2 comments · May be fixed by #14947
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented May 30, 2025

Summary

.

Lint Name

unused_trait_names

Reproducer

I tried this code:

#![warn(clippy::unused_trait_names)]
#![feature(decl_macro)]

mod m {
    pub trait Tr {
        fn method(&self) {}
    }

    impl Tr for u8 {}
}

macro gen_import($Br: ident) {
    use m::Tr as $Br;
}
gen_import!(Br);

fn main() {
    0u8.method();
}

I saw this happen:

warning: importing trait that is only used anonymously
  --> src/main.rs:13:12
   |
13 |     use m::Tr as $Br;
   |            ^^ help: use: `Tr as _`
14 | }
15 | gen_import!(Br);
   | --------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_trait_names
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::unused_trait_names)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: this warning originates in the macro `gen_import` (in Nightly builds, run with -Z macro-backtrace for more info)

suggestion

#![warn(clippy::unused_trait_names)]
#![feature(decl_macro)]

mod m {
    pub trait Tr {
        fn method(&self) {}
    }

    impl Tr for u8 {}
}

macro gen_import($Br: ident) {
    use m::Tr as _ as $Br;
}
gen_import!(Br);

fn main() {
    0u8.method();
}

does not compile

error: expected `;`, found keyword `as`
  --> src/main.rs:13:20
   |
13 |     use m::Tr as _ as $Br;
   |                    ^^ expected `;`
14 | }
15 | gen_import!(Br);
   | --------------- in this macro invocation
   |
   = note: this error originates in the macro `gen_import` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no method named `method` found for type `u8` in the current scope
  --> src/main.rs:18:9
   |
6  |         fn method(&self) {}
   |            ------ the method is available for `u8` here
...
18 |     0u8.method();
   |         ^^^^^^ method not found in `u8`error: expected `;`, found keyword `as`
  --> src/main.rs:13:20
   |
13 |     use m::Tr as _ as $Br;
   |                    ^^ expected `;`
14 | }
15 | gen_import!(Br);
   | --------------- in this macro invocation
   |
   = note: this error originates in the macro `gen_import` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no method named `method` found for type `u8` in the current scope
  --> src/main.rs:18:9
   |
6  |         fn method(&self) {}
   |            ------ the method is available for `u8` here
...
18 |     0u8.method();
   |         ^^^^^^ method not found in `u8`
   |
   = help: items from traits can only be used if the trait is in scope
help: trait `Tr` which provides `method` is implemented but not in scope; perhaps you want to import it
   |
4  + use crate::m::Tr;
   |

For more information about this error, try `rustc --explain E0599`.
   |
   = help: items from traits can only be used if the trait is in scope
help: trait `Tr` which provides `method` is implemented but not in scope; perhaps you want to import it
   |
4  + use crate::m::Tr;
   |

For more information about this error, try `rustc --explain E0599`.

Version


Additional Labels

No response

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels May 30, 2025
@matthiaskrgr
Copy link
Member Author

rustc 1.89.0-nightly (1bbd62e54 2025-05-29)
binary: rustc
commit-hash: 1bbd62e547ba5cc08ccb44c27def3d33195d2dd5
commit-date: 2025-05-29
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

@profetia
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants