Skip to content

Commit

Permalink
lint new_without_default on const fns too
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jun 7, 2023
1 parent c8a0565 commit 2891dab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
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 @@ -76,10 +76,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
4 changes: 2 additions & 2 deletions tests/ui/new_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,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 @@ -50,6 +50,23 @@ LL + }
LL + }
|

error: you should consider adding a `Default` implementation for `Const`
--> $DIR/new_without_default.rs:117: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:177:5
|
Expand Down Expand Up @@ -120,5 +137,5 @@ LL +
LL ~ impl<T> Foo<T> {
|

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

0 comments on commit 2891dab

Please sign in to comment.