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

`clippy::default_trait_access` can give incorrect suggestion

Open hirehamir opened this issue 2 years ago • 1 comments

There seems to be another issue with default_trait_access in this code.

#![warn(clippy::pedantic)]

mod data {
    #[derive(Default)]
    pub struct Data<T> {
        pub _data: T,
    }
}
fn main() {
    let _s22: data::Data<u8> = Default::default();
}

cargo clippy suggests

    Checking vectors v0.1.0 (/home/hamir/vectors)
warning: calling `Data::default()` is more clear than this expression
  --> src/main.rs:10:32
   |
10 |     let _s22: data::Data<u8> = Default::default();
   |                                ^^^^^^^^^^^^^^^^^^ help: try: `Data::default()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_trait_access
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::pedantic)]
   |         ^^^^^^^^^^^^^^^^
   = note: `#[warn(clippy::default_trait_access)]` implied by `#[warn(clippy::pedantic)]`

warning: `vectors` (bin "vectors") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s

but Data::default() results in a compilation error whereas data::Data::default() would have worked fine.

Originally posted by @hamirmahal in https://github.com/rust-lang/rust-clippy/issues/8342#issuecomment-2156213527

hirehamir avatar Jun 08 '24 22:06 hirehamir

@rustbot claim

NullSpaceBoy avatar Jun 18 '24 16:06 NullSpaceBoy