diff options
| author | bors <bors@rust-lang.org> | 2024-02-16 07:55:05 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-02-16 07:55:05 +0000 |
| commit | 61daf674eaf17f3b504c51f01b4ee63fac47dfcf (patch) | |
| tree | ca04623ea7bf9774d11e46abb1d79e9f52110244 | |
| parent | 237fbdd4da1fee6842cf5c25ab8078acb913696d (diff) | |
| parent | f0adfe74b6e83e2fea8ad46710670b29572f4885 (diff) | |
| download | rust-61daf674eaf17f3b504c51f01b4ee63fac47dfcf.tar.gz rust-61daf674eaf17f3b504c51f01b4ee63fac47dfcf.zip | |
Auto merge of #10903 - Centri3:new_without_default, r=llogiq
[`new_without_default`]: Now emits on const fns While `Default::default` is not const, it can still call `const new`; there's no reason this shouldn't be linted as well. fixes #10877 changelog: [`new_without_default`]: Now emits on const fns
| -rw-r--r-- | clippy_lints/src/new_without_default.rs | 4 | ||||
| -rw-r--r-- | tests/ui/new_without_default.fixed | 10 | ||||
| -rw-r--r-- | tests/ui/new_without_default.rs | 4 | ||||
| -rw-r--r-- | tests/ui/new_without_default.stderr | 19 |
4 files changed, 28 insertions, 9 deletions
diff --git a/clippy_lints/src/new_without_default.rs b/clippy_lints/src/new_without_default.rs index 9de6ad42137..b3b8a5e9963 100644 --- a/clippy_lints/src/new_without_default.rs +++ b/clippy_lints/src/new_without_default.rs @@ -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; diff --git a/tests/ui/new_without_default.fixed b/tests/ui/new_without_default.fixed index 1c7ba1a48c9..85408c4e17f 100644 --- a/tests/ui/new_without_default.fixed +++ b/tests/ui/new_without_default.fixed @@ -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; diff --git a/tests/ui/new_without_default.rs b/tests/ui/new_without_default.rs index 964aa0f63da..3ac7292c236 100644 --- a/tests/ui/new_without_default.rs +++ b/tests/ui/new_without_default.rs @@ -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; diff --git a/tests/ui/new_without_default.stderr b/tests/ui/new_without_default.stderr index acba5b0d7bd..6652a264205 100644 --- a/tests/ui/new_without_default.stderr +++ b/tests/ui/new_without_default.stderr @@ -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 | @@ -149,5 +166,5 @@ LL + } LL + } | -error: aborting due to 8 previous errors +error: aborting due to 9 previous errors |
