Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions clippy_lints/src/new_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -132,21 +133,23 @@ 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,
id,
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,
);
},
Expand All @@ -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)
}
12 changes: 12 additions & 0 deletions tests/ui/new_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
32 changes: 25 additions & 7 deletions tests/ui/new_without_default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,7 +24,7 @@ LL | | Bar
LL | | }
| |_____^
|
help: try this
help: try adding this
|
LL | impl Default for Bar {
LL | fn default() -> Self {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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