Skip to content

Commit 1698ce0

Browse files
committed
Auto merge of #11280 - samueltardieu:issue-11267, r=Centri3
[new_without_default]: include `where` clause in suggestions, make applicable changelog: [`new_without_default`]: include `where` clause in suggestions
2 parents 40872cb + f9b22e7 commit 1698ce0

File tree

4 files changed

+87
-11
lines changed

4 files changed

+87
-11
lines changed

clippy_lints/src/new_without_default.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
130130
}
131131

132132
let generics_sugg = snippet(cx, generics.span, "");
133+
let where_clause_sugg = if generics.has_where_clause_predicates {
134+
format!("\n{}\n", snippet(cx, generics.where_clause_span, ""))
135+
} else {
136+
String::new()
137+
};
133138
let self_ty_fmt = self_ty.to_string();
134139
let self_type_snip = snippet(cx, impl_self_ty.span, &self_ty_fmt);
135140
span_lint_hir_and_then(
@@ -145,8 +150,12 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
145150
cx,
146151
item.span,
147152
"try adding this",
148-
&create_new_without_default_suggest_msg(&self_type_snip, &generics_sugg),
149-
Applicability::MaybeIncorrect,
153+
&create_new_without_default_suggest_msg(
154+
&self_type_snip,
155+
&generics_sugg,
156+
&where_clause_sugg
157+
),
158+
Applicability::MachineApplicable,
150159
);
151160
},
152161
);
@@ -159,10 +168,14 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
159168
}
160169
}
161170

162-
fn create_new_without_default_suggest_msg(self_type_snip: &str, generics_sugg: &str) -> String {
171+
fn create_new_without_default_suggest_msg(
172+
self_type_snip: &str,
173+
generics_sugg: &str,
174+
where_clause_sugg: &str,
175+
) -> String {
163176
#[rustfmt::skip]
164177
format!(
165-
"impl{generics_sugg} Default for {self_type_snip} {{
178+
"impl{generics_sugg} Default for {self_type_snip}{where_clause_sugg} {{
166179
fn default() -> Self {{
167180
Self::new()
168181
}}

tests/ui/new_without_default.fixed

+27-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ impl<'c> LtKo<'c> {
102102
pub fn new() -> LtKo<'c> {
103103
unimplemented!()
104104
}
105-
// FIXME: that suggestion is missing lifetimes
106105
}
107106

108107
struct Private;
@@ -273,3 +272,30 @@ impl IgnoreLifetimeNew {
273272
Self
274273
}
275274
}
275+
276+
// From issue #11267
277+
278+
pub struct MyStruct<K, V>
279+
where
280+
K: std::hash::Hash + Eq + PartialEq,
281+
{
282+
_kv: Option<(K, V)>,
283+
}
284+
285+
impl<K, V> Default for MyStruct<K, V>
286+
where
287+
K: std::hash::Hash + Eq + PartialEq,
288+
{
289+
fn default() -> Self {
290+
Self::new()
291+
}
292+
}
293+
294+
impl<K, V> MyStruct<K, V>
295+
where
296+
K: std::hash::Hash + Eq + PartialEq,
297+
{
298+
pub fn new() -> Self {
299+
Self { _kv: None }
300+
}
301+
}

tests/ui/new_without_default.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ impl<'c> LtKo<'c> {
8484
pub fn new() -> LtKo<'c> {
8585
unimplemented!()
8686
}
87-
// FIXME: that suggestion is missing lifetimes
8887
}
8988

9089
struct Private;
@@ -231,3 +230,21 @@ impl IgnoreLifetimeNew {
231230
Self
232231
}
233232
}
233+
234+
// From issue #11267
235+
236+
pub struct MyStruct<K, V>
237+
where
238+
K: std::hash::Hash + Eq + PartialEq,
239+
{
240+
_kv: Option<(K, V)>,
241+
}
242+
243+
impl<K, V> MyStruct<K, V>
244+
where
245+
K: std::hash::Hash + Eq + PartialEq,
246+
{
247+
pub fn new() -> Self {
248+
Self { _kv: None }
249+
}
250+
}

tests/ui/new_without_default.stderr

+25-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ LL + }
5151
|
5252

5353
error: you should consider adding a `Default` implementation for `NewNotEqualToDerive`
54-
--> $DIR/new_without_default.rs:177:5
54+
--> $DIR/new_without_default.rs:176:5
5555
|
5656
LL | / pub fn new() -> Self {
5757
LL | | NewNotEqualToDerive { foo: 1 }
@@ -68,7 +68,7 @@ LL + }
6868
|
6969

7070
error: you should consider adding a `Default` implementation for `FooGenerics<T>`
71-
--> $DIR/new_without_default.rs:185:5
71+
--> $DIR/new_without_default.rs:184:5
7272
|
7373
LL | / pub fn new() -> Self {
7474
LL | | Self(Default::default())
@@ -85,7 +85,7 @@ LL + }
8585
|
8686

8787
error: you should consider adding a `Default` implementation for `BarGenerics<T>`
88-
--> $DIR/new_without_default.rs:192:5
88+
--> $DIR/new_without_default.rs:191:5
8989
|
9090
LL | / pub fn new() -> Self {
9191
LL | | Self(Default::default())
@@ -102,7 +102,7 @@ LL + }
102102
|
103103

104104
error: you should consider adding a `Default` implementation for `Foo<T>`
105-
--> $DIR/new_without_default.rs:203:9
105+
--> $DIR/new_without_default.rs:202:9
106106
|
107107
LL | / pub fn new() -> Self {
108108
LL | | todo!()
@@ -120,5 +120,25 @@ LL +
120120
LL ~ impl<T> Foo<T> {
121121
|
122122

123-
error: aborting due to 7 previous errors
123+
error: you should consider adding a `Default` implementation for `MyStruct<K, V>`
124+
--> $DIR/new_without_default.rs:247:5
125+
|
126+
LL | / pub fn new() -> Self {
127+
LL | | Self { _kv: None }
128+
LL | | }
129+
| |_____^
130+
|
131+
help: try adding this
132+
|
133+
LL + impl<K, V> Default for MyStruct<K, V>
134+
LL + where
135+
LL + K: std::hash::Hash + Eq + PartialEq,
136+
LL + {
137+
LL + fn default() -> Self {
138+
LL + Self::new()
139+
LL + }
140+
LL + }
141+
|
142+
143+
error: aborting due to 8 previous errors
124144

0 commit comments

Comments
 (0)