Skip to content

Commit 3214de3

Browse files
committed
Auto merge of #7493 - xFrednet:7220-fix-new-without-default-impl-type, r=camsteffen
Prefer a code snipped over formatting the self type (`new_without_default`) Fixes: #7220 changelog: [`new_without_default`]: The `Default` impl block type doesn't use the full type path qualification Have a nice day to everyone reading this 🙃
2 parents 7d6946d + 89c8c3f commit 3214de3

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

clippy_lints/src/new_without_default.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir as hir;
99
use rustc_hir::HirIdSet;
1010
use rustc_lint::{LateContext, LateLintPass, LintContext};
1111
use rustc_middle::lint::in_external_macro;
12-
use rustc_middle::ty::{Ty, TyS};
12+
use rustc_middle::ty::TyS;
1313
use rustc_session::{declare_tool_lint, impl_lint_pass};
1414
use rustc_span::sym;
1515

@@ -65,6 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
6565
if let hir::ItemKind::Impl(hir::Impl {
6666
of_trait: None,
6767
ref generics,
68+
self_ty: impl_self_ty,
6869
items,
6970
..
7071
}) = item.kind
@@ -132,21 +133,23 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
132133
}
133134

134135
let generics_sugg = snippet(cx, generics.span, "");
136+
let self_ty_fmt = self_ty.to_string();
137+
let self_type_snip = snippet(cx, impl_self_ty.span, &self_ty_fmt);
135138
span_lint_hir_and_then(
136139
cx,
137140
NEW_WITHOUT_DEFAULT,
138141
id,
139142
impl_item.span,
140143
&format!(
141144
"you should consider adding a `Default` implementation for `{}`",
142-
self_ty
145+
self_type_snip
143146
),
144147
|diag| {
145148
diag.suggest_prepend_item(
146149
cx,
147150
item.span,
148-
"try this",
149-
&create_new_without_default_suggest_msg(self_ty, &generics_sugg),
151+
"try adding this",
152+
&create_new_without_default_suggest_msg(&self_type_snip, &generics_sugg),
150153
Applicability::MaybeIncorrect,
151154
);
152155
},
@@ -160,12 +163,12 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
160163
}
161164
}
162165

163-
fn create_new_without_default_suggest_msg(ty: Ty<'_>, generics_sugg: &str) -> String {
166+
fn create_new_without_default_suggest_msg(self_type_snip: &str, generics_sugg: &str) -> String {
164167
#[rustfmt::skip]
165168
format!(
166169
"impl{} Default for {} {{
167170
fn default() -> Self {{
168171
Self::new()
169172
}}
170-
}}", generics_sugg, ty)
173+
}}", generics_sugg, self_type_snip)
171174
}

tests/ui/new_without_default.rs

+12
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,16 @@ impl<T: Copy> BarGenerics<T> {
173173
}
174174
}
175175

176+
pub mod issue7220 {
177+
pub struct Foo<T> {
178+
_bar: *mut T,
179+
}
180+
181+
impl<T> Foo<T> {
182+
pub fn new() -> Self {
183+
todo!()
184+
}
185+
}
186+
}
187+
176188
fn main() {}

tests/ui/new_without_default.stderr

+25-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | | }
77
| |_____^
88
|
99
= note: `-D clippy::new-without-default` implied by `-D warnings`
10-
help: try this
10+
help: try adding this
1111
|
1212
LL | impl Default for Foo {
1313
LL | fn default() -> Self {
@@ -24,7 +24,7 @@ LL | | Bar
2424
LL | | }
2525
| |_____^
2626
|
27-
help: try this
27+
help: try adding this
2828
|
2929
LL | impl Default for Bar {
3030
LL | fn default() -> Self {
@@ -41,7 +41,7 @@ LL | | unimplemented!()
4141
LL | | }
4242
| |_____^
4343
|
44-
help: try this
44+
help: try adding this
4545
|
4646
LL | impl<'c> Default for LtKo<'c> {
4747
LL | fn default() -> Self {
@@ -58,7 +58,7 @@ LL | | NewNotEqualToDerive { foo: 1 }
5858
LL | | }
5959
| |_____^
6060
|
61-
help: try this
61+
help: try adding this
6262
|
6363
LL | impl Default for NewNotEqualToDerive {
6464
LL | fn default() -> Self {
@@ -75,7 +75,7 @@ LL | | Self(Default::default())
7575
LL | | }
7676
| |_____^
7777
|
78-
help: try this
78+
help: try adding this
7979
|
8080
LL | impl<T> Default for FooGenerics<T> {
8181
LL | fn default() -> Self {
@@ -92,7 +92,7 @@ LL | | Self(Default::default())
9292
LL | | }
9393
| |_____^
9494
|
95-
help: try this
95+
help: try adding this
9696
|
9797
LL | impl<T: Copy> Default for BarGenerics<T> {
9898
LL | fn default() -> Self {
@@ -101,5 +101,23 @@ LL | }
101101
LL | }
102102
|
103103

104-
error: aborting due to 6 previous errors
104+
error: you should consider adding a `Default` implementation for `Foo<T>`
105+
--> $DIR/new_without_default.rs:182:9
106+
|
107+
LL | / pub fn new() -> Self {
108+
LL | | todo!()
109+
LL | | }
110+
| |_________^
111+
|
112+
help: try adding this
113+
|
114+
LL | impl<T> Default for Foo<T> {
115+
LL | fn default() -> Self {
116+
LL | Self::new()
117+
LL | }
118+
LL | }
119+
LL |
120+
...
121+
122+
error: aborting due to 7 previous errors
105123

0 commit comments

Comments
 (0)