Skip to content

Commit 9064cc8

Browse files
committed
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
2 parents 237fbdd + f0adfe7 commit 9064cc8

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

clippy_lints/src/new_without_default.rs

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

tests/ui/new_without_default.fixed

+8-2
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,18 @@ impl PrivateItem {
132132
} // We don't lint private items on public structs
133133
}
134134

135-
struct Const;
135+
pub struct Const;
136+
137+
impl Default for Const {
138+
fn default() -> Self {
139+
Self::new()
140+
}
141+
}
136142

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

143149
pub struct IgnoreGenericNew;

tests/ui/new_without_default.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ impl PrivateItem {
114114
} // We don't lint private items on public structs
115115
}
116116

117-
struct Const;
117+
pub struct Const;
118118

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

125125
pub struct IgnoreGenericNew;

tests/ui/new_without_default.stderr

+18-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ LL + }
5555
LL + }
5656
|
5757

58+
error: you should consider adding a `Default` implementation for `Const`
59+
--> $DIR/new_without_default.rs:120:5
60+
|
61+
LL | / pub const fn new() -> Const {
62+
LL | | Const
63+
LL | | } // While Default is not const, it can still call const functions, so we should lint this
64+
| |_____^
65+
|
66+
help: try adding this
67+
|
68+
LL + impl Default for Const {
69+
LL + fn default() -> Self {
70+
LL + Self::new()
71+
LL + }
72+
LL + }
73+
|
74+
5875
error: you should consider adding a `Default` implementation for `NewNotEqualToDerive`
5976
--> $DIR/new_without_default.rs:180:5
6077
|
@@ -149,5 +166,5 @@ LL + }
149166
LL + }
150167
|
151168

152-
error: aborting due to 8 previous errors
169+
error: aborting due to 9 previous errors
153170

0 commit comments

Comments
 (0)