Skip to content
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

only lint new_without_default for public items #972

Merged
merged 1 commit into from
Jun 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {}