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

new_without_default does not include generic bounds in suggestions #11267

Closed
abcalphabet opened this issue Jul 31, 2023 · 1 comment · Fixed by #11280
Closed

new_without_default does not include generic bounds in suggestions #11267

abcalphabet opened this issue Jul 31, 2023 · 1 comment · Fixed by #11280
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@abcalphabet
Copy link

abcalphabet commented Jul 31, 2023

Summary

new_without_default suggests the implementation of the Default trait on generics without the specified trait bounds. This leads to the suggested change not compiling.

Playground example

Reproducer

I tried this code:

use std::collections::HashMap;
use std::hash::Hash;

pub struct MyStruct<K, V>
where
    K: Hash + Eq + PartialEq 
{
    _map: HashMap<K, V>,
}

impl<K, V> MyStruct<K, V>
where
    K: Hash + Eq + PartialEq 
{
    pub fn new() -> Self {
        Self {
            _map: HashMap::new(),
        }
    }
}

I expected to see this happen:

Either no warning, or

warning: you should consider adding a `Default` implementation for `MyStruct<K, V>`
  --> src/lib.rs:15:5
   |
15 | /     pub fn new() -> Self {
16 | |         Self {
17 | |             _map: HashMap::new(),
18 | |         }
19 | |     }
   | |_____^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default
   = note: `#[warn(clippy::new_without_default)]` on by default
help: try adding this
   |
11 + impl<K, V> Default for MyStruct<K, V>
12 + where
13 +     K: Hash + Eq + PartialEq  {
14 +     fn default() -> Self {
15 +         Self::new()
16 +     }
17 + }

Instead, this happened:

warning: you should consider adding a `Default` implementation for `MyStruct<K, V>`
  --> src/lib.rs:15:5
   |
15 | /     pub fn new() -> Self {
16 | |         Self {
17 | |             _map: HashMap::new(),
18 | |         }
19 | |     }
   | |_____^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default
   = note: `#[warn(clippy::new_without_default)]` on by default
help: try adding this
   |
11 + impl<K, V> Default for MyStruct<K, V> {
12 +     fn default() -> Self {
13 +         Self::new()
14 +     }
15 + }

Version

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: aarch64-apple-darwin
release: 1.71.0
LLVM version: 16.0.5

Additional Labels

No response

@abcalphabet abcalphabet added the C-bug Category: Clippy is not doing the correct thing label Jul 31, 2023
@samueltardieu
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
Projects
None yet
2 participants