mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-14 00:47:16 +00:00
Prefer a code snipped over formatting the self type (new_without_default
)
This commit is contained in:
parent
ea69a9d10a
commit
89c8c3f4cd
3 changed files with 46 additions and 13 deletions
|
@ -9,7 +9,7 @@ use rustc_hir as hir;
|
||||||
use rustc_hir::HirIdSet;
|
use rustc_hir::HirIdSet;
|
||||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||||
use rustc_middle::lint::in_external_macro;
|
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_session::{declare_tool_lint, impl_lint_pass};
|
||||||
use rustc_span::sym;
|
use rustc_span::sym;
|
||||||
|
|
||||||
|
@ -65,6 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
|
||||||
if let hir::ItemKind::Impl(hir::Impl {
|
if let hir::ItemKind::Impl(hir::Impl {
|
||||||
of_trait: None,
|
of_trait: None,
|
||||||
ref generics,
|
ref generics,
|
||||||
|
self_ty: impl_self_ty,
|
||||||
items,
|
items,
|
||||||
..
|
..
|
||||||
}) = item.kind
|
}) = item.kind
|
||||||
|
@ -132,6 +133,8 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
|
||||||
}
|
}
|
||||||
|
|
||||||
let generics_sugg = snippet(cx, generics.span, "");
|
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(
|
span_lint_hir_and_then(
|
||||||
cx,
|
cx,
|
||||||
NEW_WITHOUT_DEFAULT,
|
NEW_WITHOUT_DEFAULT,
|
||||||
|
@ -139,14 +142,14 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
|
||||||
impl_item.span,
|
impl_item.span,
|
||||||
&format!(
|
&format!(
|
||||||
"you should consider adding a `Default` implementation for `{}`",
|
"you should consider adding a `Default` implementation for `{}`",
|
||||||
self_ty
|
self_type_snip
|
||||||
),
|
),
|
||||||
|diag| {
|
|diag| {
|
||||||
diag.suggest_prepend_item(
|
diag.suggest_prepend_item(
|
||||||
cx,
|
cx,
|
||||||
item.span,
|
item.span,
|
||||||
"try this",
|
"try adding this",
|
||||||
&create_new_without_default_suggest_msg(self_ty, &generics_sugg),
|
&create_new_without_default_suggest_msg(&self_type_snip, &generics_sugg),
|
||||||
Applicability::MaybeIncorrect,
|
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]
|
#[rustfmt::skip]
|
||||||
format!(
|
format!(
|
||||||
"impl{} Default for {} {{
|
"impl{} Default for {} {{
|
||||||
fn default() -> Self {{
|
fn default() -> Self {{
|
||||||
Self::new()
|
Self::new()
|
||||||
}}
|
}}
|
||||||
}}", generics_sugg, ty)
|
}}", generics_sugg, self_type_snip)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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() {}
|
fn main() {}
|
||||||
|
|
|
@ -7,7 +7,7 @@ LL | | }
|
||||||
| |_____^
|
| |_____^
|
||||||
|
|
|
|
||||||
= note: `-D clippy::new-without-default` implied by `-D warnings`
|
= note: `-D clippy::new-without-default` implied by `-D warnings`
|
||||||
help: try this
|
help: try adding this
|
||||||
|
|
|
|
||||||
LL | impl Default for Foo {
|
LL | impl Default for Foo {
|
||||||
LL | fn default() -> Self {
|
LL | fn default() -> Self {
|
||||||
|
@ -24,7 +24,7 @@ LL | | Bar
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_____^
|
| |_____^
|
||||||
|
|
|
|
||||||
help: try this
|
help: try adding this
|
||||||
|
|
|
|
||||||
LL | impl Default for Bar {
|
LL | impl Default for Bar {
|
||||||
LL | fn default() -> Self {
|
LL | fn default() -> Self {
|
||||||
|
@ -41,7 +41,7 @@ LL | | unimplemented!()
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_____^
|
| |_____^
|
||||||
|
|
|
|
||||||
help: try this
|
help: try adding this
|
||||||
|
|
|
|
||||||
LL | impl<'c> Default for LtKo<'c> {
|
LL | impl<'c> Default for LtKo<'c> {
|
||||||
LL | fn default() -> Self {
|
LL | fn default() -> Self {
|
||||||
|
@ -58,7 +58,7 @@ LL | | NewNotEqualToDerive { foo: 1 }
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_____^
|
| |_____^
|
||||||
|
|
|
|
||||||
help: try this
|
help: try adding this
|
||||||
|
|
|
|
||||||
LL | impl Default for NewNotEqualToDerive {
|
LL | impl Default for NewNotEqualToDerive {
|
||||||
LL | fn default() -> Self {
|
LL | fn default() -> Self {
|
||||||
|
@ -75,7 +75,7 @@ LL | | Self(Default::default())
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_____^
|
| |_____^
|
||||||
|
|
|
|
||||||
help: try this
|
help: try adding this
|
||||||
|
|
|
|
||||||
LL | impl<T> Default for FooGenerics<T> {
|
LL | impl<T> Default for FooGenerics<T> {
|
||||||
LL | fn default() -> Self {
|
LL | fn default() -> Self {
|
||||||
|
@ -92,7 +92,7 @@ LL | | Self(Default::default())
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_____^
|
| |_____^
|
||||||
|
|
|
|
||||||
help: try this
|
help: try adding this
|
||||||
|
|
|
|
||||||
LL | impl<T: Copy> Default for BarGenerics<T> {
|
LL | impl<T: Copy> Default for BarGenerics<T> {
|
||||||
LL | fn default() -> Self {
|
LL | fn default() -> Self {
|
||||||
|
@ -101,5 +101,23 @@ LL | }
|
||||||
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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue