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

[new_without_default]: Now emits on const fns #10903

Merged
merged 1 commit into from
Feb 16, 2024
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
4 changes: 0 additions & 4 deletions clippy_lints/src/new_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
if let hir::ImplItemKind::Fn(ref sig, _) = impl_item.kind {
let name = impl_item.ident.name;
let id = impl_item.owner_id;
if sig.header.constness == hir::Constness::Const {
// can't be implemented by default
return;
}
if sig.header.unsafety == hir::Unsafety::Unsafe {
// can't be implemented for unsafe new
return;
Expand Down
10 changes: 8 additions & 2 deletions tests/ui/new_without_default.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,18 @@ impl PrivateItem {
} // We don't lint private items on public structs
}

struct Const;
pub struct Const;

impl Default for Const {
fn default() -> Self {
Self::new()
}
}

impl Const {
pub const fn new() -> Const {
Const
} // const fns can't be implemented via Default
} // While Default is not const, it can still call const functions, so we should lint this
}

pub struct IgnoreGenericNew;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/new_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ impl PrivateItem {
} // We don't lint private items on public structs
}

struct Const;
pub struct Const;

impl Const {
pub const fn new() -> Const {
Const
} // const fns can't be implemented via Default
} // While Default is not const, it can still call const functions, so we should lint this
}

pub struct IgnoreGenericNew;
Expand Down
19 changes: 18 additions & 1 deletion tests/ui/new_without_default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ LL + }
LL + }
|

error: you should consider adding a `Default` implementation for `Const`
--> $DIR/new_without_default.rs:120:5
|
LL | / pub const fn new() -> Const {
LL | | Const
LL | | } // While Default is not const, it can still call const functions, so we should lint this
| |_____^
|
help: try adding this
|
LL + impl Default for Const {
LL + fn default() -> Self {
LL + Self::new()
LL + }
LL + }
|

error: you should consider adding a `Default` implementation for `NewNotEqualToDerive`
--> $DIR/new_without_default.rs:180:5
|
Expand Down Expand Up @@ -149,5 +166,5 @@ LL + }
LL + }
|

error: aborting due to 8 previous errors
error: aborting due to 9 previous errors

Loading