Skip to content

Commit 2891dab

Browse files
committed
lint new_without_default on const fns too
1 parent c8a0565 commit 2891dab

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

clippy_lints/src/new_without_default.rs

-4
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
7676
if let hir::ImplItemKind::Fn(ref sig, _) = impl_item.kind {
7777
let name = impl_item.ident.name;
7878
let id = impl_item.owner_id;
79-
if sig.header.constness == hir::Constness::Const {
80-
// can't be implemented by default
81-
return;
82-
}
8379
if sig.header.unsafety == hir::Unsafety::Unsafe {
8480
// can't be implemented for unsafe new
8581
return;

tests/ui/new_without_default.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ impl PrivateItem {
111111
} // We don't lint private items on public structs
112112
}
113113

114-
struct Const;
114+
pub struct Const;
115115

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

122122
pub struct IgnoreGenericNew;

tests/ui/new_without_default.stderr

+18-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ LL + }
5050
LL + }
5151
|
5252

53+
error: you should consider adding a `Default` implementation for `Const`
54+
--> $DIR/new_without_default.rs:117:5
55+
|
56+
LL | / pub const fn new() -> Const {
57+
LL | | Const
58+
LL | | } // While Default is not const, it can still call const functions, so we should lint this
59+
| |_____^
60+
|
61+
help: try adding this
62+
|
63+
LL + impl Default for Const {
64+
LL + fn default() -> Self {
65+
LL + Self::new()
66+
LL + }
67+
LL + }
68+
|
69+
5370
error: you should consider adding a `Default` implementation for `NewNotEqualToDerive`
5471
--> $DIR/new_without_default.rs:177:5
5572
|
@@ -120,5 +137,5 @@ LL +
120137
LL ~ impl<T> Foo<T> {
121138
|
122139

123-
error: aborting due to 7 previous errors
140+
error: aborting due to 8 previous errors
124141

0 commit comments

Comments
 (0)