4 files changed +28
-9
lines changed Original file line number Diff line number Diff line change @@ -75,10 +75,6 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
75
75
if let hir:: ImplItemKind :: Fn ( ref sig, _) = impl_item. kind {
76
76
let name = impl_item. ident . name ;
77
77
let id = impl_item. owner_id ;
78
- if sig. header . constness == hir:: Constness :: Const {
79
- // can't be implemented by default
80
- return ;
81
- }
82
78
if sig. header . unsafety == hir:: Unsafety :: Unsafe {
83
79
// can't be implemented for unsafe new
84
80
return ;
Original file line number Diff line number Diff line change @@ -132,12 +132,18 @@ impl PrivateItem {
132
132
} // We don't lint private items on public structs
133
133
}
134
134
135
- struct Const;
135
+ pub struct Const;
136
+
137
+ impl Default for Const {
138
+ fn default() -> Self {
139
+ Self::new()
140
+ }
141
+ }
136
142
137
143
impl Const {
138
144
pub const fn new() -> Const {
139
145
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
141
147
}
142
148
143
149
pub struct IgnoreGenericNew;
Original file line number Diff line number Diff line change @@ -114,12 +114,12 @@ impl PrivateItem {
114
114
} // We don't lint private items on public structs
115
115
}
116
116
117
- struct Const ;
117
+ pub struct Const ;
118
118
119
119
impl Const {
120
120
pub const fn new ( ) -> Const {
121
121
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
123
123
}
124
124
125
125
pub struct IgnoreGenericNew ;
Original file line number Diff line number Diff line change @@ -55,6 +55,23 @@ LL + }
55
55
LL + }
56
56
|
57
57
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
+
58
75
error: you should consider adding a `Default` implementation for `NewNotEqualToDerive`
59
76
--> $DIR/new_without_default.rs:180:5
60
77
|
@@ -149,5 +166,5 @@ LL + }
149
166
LL + }
150
167
|
151
168
152
- error: aborting due to 8 previous errors
169
+ error: aborting due to 9 previous errors
153
170
0 commit comments