Prefer a code snipped over formatting the self type (new_without_default)

This commit is contained in:
xFrednet 2021-07-25 20:35:51 +02:00
parent ea69a9d10a
commit 89c8c3f4cd
3 changed files with 46 additions and 13 deletions

View File

@ -9,7 +9,7 @@ use rustc_hir as hir;
use rustc_hir::HirIdSet;
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{Ty, TyS};
use rustc_middle::ty::TyS;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::sym;
@ -65,6 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
if let hir::ItemKind::Impl(hir::Impl {
of_trait: None,
ref generics,
self_ty: impl_self_ty,
items,
..
}) = item.kind
@ -132,6 +133,8 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
}
let generics_sugg = snippet(cx, generics.span, "");
let self_ty_fmt = self_ty.to_string();
let self_type_snip = snippet(cx, impl_self_ty.span, &self_ty_fmt);
span_lint_hir_and_then(
cx,
NEW_WITHOUT_DEFAULT,
@ -139,14 +142,14 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
impl_item.span,
&format!(
"you should consider adding a `Default` implementation for `{}`",
self_ty
self_type_snip
),
|diag| {
diag.suggest_prepend_item(
cx,
item.span,
"try this",
&create_new_without_default_suggest_msg(self_ty, &generics_sugg),
"try adding this",
&create_new_without_default_suggest_msg(&self_type_snip, &generics_sugg),
Applicability::MaybeIncorrect,
);
},
@ -160,12 +163,12 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
}
}
fn create_new_without_default_suggest_msg(ty: Ty<'_>, generics_sugg: &str) -> String {
fn create_new_without_default_suggest_msg(self_type_snip: &str, generics_sugg: &str) -> String {
#[rustfmt::skip]
format!(
"impl{} Default for {} {{
fn default() -> Self {{
Self::new()
}}
}}", generics_sugg, ty)
}}", generics_sugg, self_type_snip)
}

View File

@ -173,4 +173,16 @@ impl<T: Copy> BarGenerics<T> {
}
}
pub mod issue7220 {
pub struct Foo<T> {
_bar: *mut T,
}
impl<T> Foo<T> {
pub fn new() -> Self {
todo!()
}
}
}
fn main() {}

View File

@ -7,7 +7,7 @@ LL | | }
| |_____^
|
= note: `-D clippy::new-without-default` implied by `-D warnings`
help: try this
help: try adding this
|
LL | impl Default for Foo {
LL | fn default() -> Self {
@ -24,7 +24,7 @@ LL | | Bar
LL | | }
| |_____^
|
help: try this
help: try adding this
|
LL | impl Default for Bar {
LL | fn default() -> Self {
@ -41,7 +41,7 @@ LL | | unimplemented!()
LL | | }
| |_____^
|
help: try this
help: try adding this
|
LL | impl<'c> Default for LtKo<'c> {
LL | fn default() -> Self {
@ -58,7 +58,7 @@ LL | | NewNotEqualToDerive { foo: 1 }
LL | | }
| |_____^
|
help: try this
help: try adding this
|
LL | impl Default for NewNotEqualToDerive {
LL | fn default() -> Self {
@ -75,7 +75,7 @@ LL | | Self(Default::default())
LL | | }
| |_____^
|
help: try this
help: try adding this
|
LL | impl<T> Default for FooGenerics<T> {
LL | fn default() -> Self {
@ -92,7 +92,7 @@ LL | | Self(Default::default())
LL | | }
| |_____^
|
help: try this
help: try adding this
|
LL | impl<T: Copy> Default for BarGenerics<T> {
LL | fn default() -> Self {
@ -101,5 +101,23 @@ LL | }
LL | }
|
error: aborting due to 6 previous errors
error: you should consider adding a `Default` implementation for `Foo<T>`
--> $DIR/new_without_default.rs:182:9
|
LL | / pub fn new() -> Self {
LL | | todo!()
LL | | }
| |_________^
|
help: try adding this
|
LL | impl<T> Default for Foo<T> {
LL | fn default() -> Self {
LL | Self::new()
LL | }
LL | }
LL |
...
error: aborting due to 7 previous errors