diff --git a/clippy_lints/src/new_without_default.rs b/clippy_lints/src/new_without_default.rs index 08d517014ee..15bb782452e 100644 --- a/clippy_lints/src/new_without_default.rs +++ b/clippy_lints/src/new_without_default.rs @@ -96,7 +96,8 @@ impl LateLintPass for NewWithoutDefault { } if let FnKind::Method(name, _, _, _) = kind { - if decl.inputs.is_empty() && name.as_str() == "new" { + if decl.inputs.is_empty() && name.as_str() == "new" && + cx.access_levels.is_reachable(id) { let self_ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id( cx.tcx.map.get_parent(id))).ty; if_let_chain!{[ diff --git a/tests/compile-fail/new_without_default.rs b/tests/compile-fail/new_without_default.rs index 17f2a8d7b41..042fab840c1 100644 --- a/tests/compile-fail/new_without_default.rs +++ b/tests/compile-fail/new_without_default.rs @@ -4,35 +4,35 @@ #![allow(dead_code)] #![deny(new_without_default, new_without_default_derive)] -struct Foo; +pub struct Foo; impl Foo { - fn new() -> Foo { Foo } //~ERROR: you should consider deriving a `Default` implementation for `Foo` + pub fn new() -> Foo { Foo } //~ERROR: you should consider deriving a `Default` implementation for `Foo` } -struct Bar; +pub struct Bar; impl Bar { - fn new() -> Self { Bar } //~ERROR: you should consider deriving a `Default` implementation for `Bar` + pub fn new() -> Self { Bar } //~ERROR: you should consider deriving a `Default` implementation for `Bar` } -struct Ok; +pub struct Ok; impl Ok { - fn new() -> Self { Ok } + pub fn new() -> Self { Ok } } impl Default for Ok { fn default() -> Self { Ok } } -struct Params; +pub struct Params; impl Params { - fn new(_: u32) -> Self { Params } + pub fn new(_: u32) -> Self { Params } } -struct GenericsOk { +pub struct GenericsOk { bar: T, } @@ -41,10 +41,10 @@ impl Default for GenericsOk { } impl<'c, V> GenericsOk { - fn new() -> GenericsOk { unimplemented!() } + pub fn new() -> GenericsOk { unimplemented!() } } -struct LtOk<'a> { +pub struct LtOk<'a> { foo: &'a bool, } @@ -53,15 +53,21 @@ impl<'b> Default for LtOk<'b> { } impl<'c> LtOk<'c> { - fn new() -> LtOk<'c> { unimplemented!() } + pub fn new() -> LtOk<'c> { unimplemented!() } } -struct LtKo<'a> { +pub struct LtKo<'a> { foo: &'a bool, } impl<'c> LtKo<'c> { - fn new() -> LtKo<'c> { unimplemented!() } //~ERROR: you should consider adding a `Default` implementation for + pub fn new() -> LtKo<'c> { unimplemented!() } //~ERROR: you should consider adding a `Default` implementation for +} + +struct Private; + +impl Private { + fn new() -> Private { unimplemented!() } // We don't lint private items } fn main() {}