From 89c8c3f4cdf4bc3289007d52932564dea6b3bf6d Mon Sep 17 00:00:00 2001 From: xFrednet Date: Sun, 25 Jul 2021 20:35:51 +0200 Subject: [PATCH] Prefer a code snipped over formatting the self type (`new_without_default`) --- clippy_lints/src/new_without_default.rs | 15 +++++++----- tests/ui/new_without_default.rs | 12 ++++++++++ tests/ui/new_without_default.stderr | 32 +++++++++++++++++++------ 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/new_without_default.rs b/clippy_lints/src/new_without_default.rs index a5f91eb035f..ae769c82081 100644 --- a/clippy_lints/src/new_without_default.rs +++ b/clippy_lints/src/new_without_default.rs @@ -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) } diff --git a/tests/ui/new_without_default.rs b/tests/ui/new_without_default.rs index 58094646b50..4b2e7444dcf 100644 --- a/tests/ui/new_without_default.rs +++ b/tests/ui/new_without_default.rs @@ -173,4 +173,16 @@ impl BarGenerics { } } +pub mod issue7220 { + pub struct Foo { + _bar: *mut T, + } + + impl Foo { + pub fn new() -> Self { + todo!() + } + } +} + fn main() {} diff --git a/tests/ui/new_without_default.stderr b/tests/ui/new_without_default.stderr index 56c5fe1c618..7c964000807 100644 --- a/tests/ui/new_without_default.stderr +++ b/tests/ui/new_without_default.stderr @@ -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 Default for FooGenerics { LL | fn default() -> Self { @@ -92,7 +92,7 @@ LL | | Self(Default::default()) LL | | } | |_____^ | -help: try this +help: try adding this | LL | impl Default for BarGenerics { 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` + --> $DIR/new_without_default.rs:182:9 + | +LL | / pub fn new() -> Self { +LL | | todo!() +LL | | } + | |_________^ + | +help: try adding this + | +LL | impl Default for Foo { +LL | fn default() -> Self { +LL | Self::new() +LL | } +LL | } +LL | + ... + +error: aborting due to 7 previous errors