Skip to content

Commit

Permalink
Merge pull request #972 from Manishearth/fix-953
Browse files Browse the repository at this point in the history
only lint `new_without_default` for public items
  • Loading branch information
mcarton committed Jun 2, 2016
2 parents efeba8e + 4998203 commit b33abd3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/new_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!{[
Expand Down
34 changes: 20 additions & 14 deletions tests/compile-fail/new_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
pub struct GenericsOk<T> {
bar: T,
}

Expand All @@ -41,10 +41,10 @@ impl<U> Default for GenericsOk<U> {
}

impl<'c, V> GenericsOk<V> {
fn new() -> GenericsOk<V> { unimplemented!() }
pub fn new() -> GenericsOk<V> { unimplemented!() }
}

struct LtOk<'a> {
pub struct LtOk<'a> {
foo: &'a bool,
}

Expand All @@ -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() {}

0 comments on commit b33abd3

Please sign in to comment.