Skip to content

Commit 478f258

Browse files
committed
Auto merge of #6939 - ThibsG:suggNewWithoutDefault, r=llogiq
Fix bad suggestion for generics in `new_without_default` lint Fixes bad suggestion where a type parameter was missing for `new_without_default` lint. Fixes #6933 changelog: none
2 parents 1d3c539 + 296751f commit 478f258

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

clippy_lints/src/new_without_default.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::paths;
3+
use clippy_utils::source::snippet;
34
use clippy_utils::sugg::DiagnosticBuilderExt;
45
use clippy_utils::{get_trait_def_id, return_ty};
56
use if_chain::if_chain;
@@ -62,7 +63,10 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
6263
#[allow(clippy::too_many_lines)]
6364
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
6465
if let hir::ItemKind::Impl(hir::Impl {
65-
of_trait: None, items, ..
66+
of_trait: None,
67+
ref generics,
68+
items,
69+
..
6670
}) = item.kind
6771
{
6872
for assoc_item in items {
@@ -126,6 +130,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
126130
}
127131
}
128132

133+
let generics_sugg = snippet(cx, generics.span, "");
129134
span_lint_hir_and_then(
130135
cx,
131136
NEW_WITHOUT_DEFAULT,
@@ -140,7 +145,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
140145
cx,
141146
item.span,
142147
"try this",
143-
&create_new_without_default_suggest_msg(self_ty),
148+
&create_new_without_default_suggest_msg(self_ty, &generics_sugg),
144149
Applicability::MaybeIncorrect,
145150
);
146151
},
@@ -155,12 +160,12 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
155160
}
156161
}
157162

158-
fn create_new_without_default_suggest_msg(ty: Ty<'_>) -> String {
163+
fn create_new_without_default_suggest_msg(ty: Ty<'_>, generics_sugg: &str) -> String {
159164
#[rustfmt::skip]
160165
format!(
161-
"impl Default for {} {{
166+
"impl{} Default for {} {{
162167
fn default() -> Self {{
163168
Self::new()
164169
}}
165-
}}", ty)
170+
}}", generics_sugg, ty)
166171
}

tests/ui/new_without_default.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,19 @@ impl NewNotEqualToDerive {
159159
}
160160
}
161161

162+
// see #6933
163+
pub struct FooGenerics<T>(std::marker::PhantomData<T>);
164+
impl<T> FooGenerics<T> {
165+
pub fn new() -> Self {
166+
Self(Default::default())
167+
}
168+
}
169+
170+
pub struct BarGenerics<T>(std::marker::PhantomData<T>);
171+
impl<T: Copy> BarGenerics<T> {
172+
pub fn new() -> Self {
173+
Self(Default::default())
174+
}
175+
}
176+
162177
fn main() {}

tests/ui/new_without_default.stderr

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ LL | | }
4343
|
4444
help: try this
4545
|
46-
LL | impl Default for LtKo<'c> {
46+
LL | impl<'c> Default for LtKo<'c> {
4747
LL | fn default() -> Self {
4848
LL | Self::new()
4949
LL | }
@@ -67,5 +67,39 @@ LL | }
6767
LL | }
6868
|
6969

70-
error: aborting due to 4 previous errors
70+
error: you should consider adding a `Default` implementation for `FooGenerics<T>`
71+
--> $DIR/new_without_default.rs:165:5
72+
|
73+
LL | / pub fn new() -> Self {
74+
LL | | Self(Default::default())
75+
LL | | }
76+
| |_____^
77+
|
78+
help: try this
79+
|
80+
LL | impl<T> Default for FooGenerics<T> {
81+
LL | fn default() -> Self {
82+
LL | Self::new()
83+
LL | }
84+
LL | }
85+
|
86+
87+
error: you should consider adding a `Default` implementation for `BarGenerics<T>`
88+
--> $DIR/new_without_default.rs:172:5
89+
|
90+
LL | / pub fn new() -> Self {
91+
LL | | Self(Default::default())
92+
LL | | }
93+
| |_____^
94+
|
95+
help: try this
96+
|
97+
LL | impl<T: Copy> Default for BarGenerics<T> {
98+
LL | fn default() -> Self {
99+
LL | Self::new()
100+
LL | }
101+
LL | }
102+
|
103+
104+
error: aborting due to 6 previous errors
71105

0 commit comments

Comments
 (0)