diff options
| author | bors <bors@rust-lang.org> | 2021-07-26 12:20:54 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-07-26 12:20:54 +0000 |
| commit | 3214de3fe6fd999274d2ccbacfc378299d53cde5 (patch) | |
| tree | 8ea54fea4f534b730a5711c6d78811fe2bfd5b8f | |
| parent | 7d6946df34f74e75a2048fee5e3eb48236028b49 (diff) | |
| parent | 89c8c3f4cdf4bc3289007d52932564dea6b3bf6d (diff) | |
| download | rust-3214de3fe6fd999274d2ccbacfc378299d53cde5.tar.gz rust-3214de3fe6fd999274d2ccbacfc378299d53cde5.zip | |
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: rust-lang/rust-clippy#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 :upside_down_face:
| -rw-r--r-- | clippy_lints/src/new_without_default.rs | 15 | ||||
| -rw-r--r-- | tests/ui/new_without_default.rs | 12 | ||||
| -rw-r--r-- | 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<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() {} 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<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 |
