-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn'tI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have
Description
Summary
I'm not sure if this is a false positive or a false negative, but it's surprising and probably a bug.
Lint Name
new_without_default
Reproducer
I tried this code:
#![allow(dead_code)]
mod foo {
pub struct Foo(Vec<i32>);
impl Foo {
pub fn new() -> Self {
Self(Vec::new())
}
}
}
I saw this happen:
$ cargo clippy
# No output
Then I modified it to add impl<'a> IntoIterator for &'a Foo
#![allow(dead_code)]
mod foo {
pub struct Foo(Vec<i32>);
impl Foo {
pub fn new() -> Self {
Self(Vec::new())
}
}
// Uncommenting this triggers clippy.
impl<'a> IntoIterator for &'a Foo {
type Item = &'a i32;
type IntoIter = std::slice::Iter<'a, i32>;
fn into_iter(self) -> Self::IntoIter {
self.0.as_slice().iter()
}
}
}
Which causes clippy to output:
warning: you should consider adding a `Default` implementation for `Foo`
--> src/lib.rs:8:9
|
8 | / pub fn new() -> Self {
9 | | Self(Vec::new())
10 | | }
| |_________^
|
= 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
|
7 ~ impl Default for Foo {
8 + fn default() -> Self {
9 + Self::new()
10 + }
11 + }
12 +
13 ~ impl Foo {
|
I expected to see this happen:
Clippy should either lint in both of these cases, or none of these. By doing it in only 1 place, I needed to add a Default
impl while making an unrelated change.
Version
0.1.92 (2025-09-28 c8905eaa66)
Additional Labels
No response
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn'tI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have