-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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_derive
triggers on types which cannot #[derive(Default)]
#1890
Comments
The idea of that lint is not to necessarily use derive, but to implement Default by forwarding to new |
If that's the case, it should probably not include |
Oh sorry. I confused the two |
Can you give an example struct where the lint triggers but shouldn't? I tried struct Foo<T>(std::marker::PhantomData<T>);
struct Bar<T>(Foo<T>);
impl<T> Bar<T> {
fn new() -> Self { unimplemented!() }
} but clippy doesn't trigger on it. |
use std::collections::HashMap;
trait SomeTrait {
type SomeType;
}
#[derive(Hash, PartialEq, Eq)]
struct Key<T: SomeTrait> {
foo: T::SomeType,
}
struct Cache<T: SomeTrait> {
foo: HashMap<Key<T>, ()>,
}
impl<T> Cache<T>
where
T: SomeTrait,
T::SomeType: Hash + Eq,
{
pub fn new() -> Self {
Cache {
foo: HashMap::new(),
}
}
} |
Unfortunately I cannot reproduce: http://play.integer32.com/?gist=56b78c856f90a1f8e0bb9e8ee50a392a&version=stable or http://play.integer32.com/?gist=8179a9caaadbd71712a54d804fea13e5&version=stable (your code as it is doesn't compile, so I had to guess) |
Sorry about that, this is the code which compiles: use std::collections::HashMap;
use std::hash::Hash;
trait SomeTrait {
type SomeType;
}
#[derive(Hash, PartialEq, Eq)]
struct Key<T: SomeTrait> {
foo: T::SomeType,
}
struct Cache<T: SomeTrait> {
foo: HashMap<Key<T>, ()>,
}
impl<T> Cache<T>
where
T: SomeTrait,
Key<T>: Hash + Eq,
{
pub fn new() -> Self {
Cache {
foo: HashMap::new(),
}
}
}
fn main() {
} |
But it doesn't trigger the lint. At least not on play.integer32.com |
The actual type which 100% triggers the lint and cannot derive Default which I was attempting to simplify is here |
I consider this a bug in derives, not in clippy: rust-lang/rust#9607 minimal repro: use std::hash::Hash;
use std::collections::HashMap;
pub struct Foo<T> {
bar: HashMap<T, usize>,
}
impl<T: Hash + Eq> Foo<T> {
pub fn new() -> Self {
Foo {
bar: HashMap::default(),
}
}
} |
I'm not sure if this should be a separate issue or not, but I feel like this code should not trigger this lint: #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct MapID(Uuid);
impl MapID {
pub fn new() -> MapID {
MapID(Uuid::new_v4())
}
pub fn to_string(&self) -> String {
self.0.hyphenated().to_string()
}
} IMO it should only trigger it when the contents of the |
For anyone who happened to be here: By the way, the repro by oli-obk can derive |
The lint seems to trigger on any function with the signature
fn new() -> Self
.#[derive(Default)]
requires a default bound on all type parameters, even if those types do not appear in the actual type body.The text was updated successfully, but these errors were encountered: