Skip to content

new_without_default for private type changed by adding trait impl #15778

@alona-em

Description

@alona-em

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 {
   |

playground link

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 thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions