From 8701009860273828cb6853e1dba79a82e1271619 Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Wed, 3 May 2023 20:06:29 +0200 Subject: [PATCH] add more test cases --- .../src/default_constructed_unit_structs.rs | 8 ++-- tests/ui/default_constructed_unit_structs.rs | 45 +++++++++++++++++++ .../default_constructed_unit_structs.stderr | 20 ++++++--- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/default_constructed_unit_structs.rs b/clippy_lints/src/default_constructed_unit_structs.rs index a79923e9715..dab76a2c41f 100644 --- a/clippy_lints/src/default_constructed_unit_structs.rs +++ b/clippy_lints/src/default_constructed_unit_structs.rs @@ -42,14 +42,14 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs { if_chain!( // make sure we have a call to `Default::default` if let hir::ExprKind::Call(fn_expr, &[]) = expr.kind; - if let ExprKind::Path(ref qpath) = fn_expr.kind; + if let ExprKind::Path(ref qpath@ hir::QPath::TypeRelative(_,_)) = fn_expr.kind; if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id); if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD); // make sure we have a struct with no fields (unit struct) if let ty::Adt(def, ..) = cx.typeck_results().expr_ty(expr).kind(); - if def.is_struct() && def.is_payloadfree() - && !def.non_enum_variant().is_field_list_non_exhaustive() - && !is_from_proc_macro(cx, expr); + if def.is_struct(); + if let var @ ty::VariantDef { ctor: Some((hir::def::CtorKind::Const, _)), .. } = def.non_enum_variant(); + if !var.is_field_list_non_exhaustive() && !is_from_proc_macro(cx, expr); then { span_lint_and_sugg( cx, diff --git a/tests/ui/default_constructed_unit_structs.rs b/tests/ui/default_constructed_unit_structs.rs index 505be09a50e..276fc40aa18 100644 --- a/tests/ui/default_constructed_unit_structs.rs +++ b/tests/ui/default_constructed_unit_structs.rs @@ -5,9 +5,23 @@ use std::marker::PhantomData; #[derive(Default)] struct UnitStruct; +impl UnitStruct { + fn new() -> Self { + //should lint + Self::default() + } +} + #[derive(Default)] struct TupleStruct(usize); +impl TupleStruct { + fn new() -> Self { + // should not lint + Self(Default::default()) + } +} + // no lint for derived impl #[derive(Default)] struct NormalStruct { @@ -39,6 +53,13 @@ impl NormalStruct { inner: PhantomData::default(), } } + + fn new2() -> Self { + // should not lint + Self { + inner: Default::default(), + } + } } #[derive(Default)] @@ -51,8 +72,29 @@ impl GenericStruct { // should not lint Self { t: T::default() } } + + fn new2() -> Self { + // should not lint + Self { t: Default::default() } + } } +struct FakeDefault; +impl FakeDefault { + fn default() -> Self { + Self + } +} + +impl Default for FakeDefault { + fn default() -> Self { + Self + } +} + +#[derive(Default)] +struct EmptyStruct {} + #[derive(Default)] #[non_exhaustive] struct NonExhaustiveStruct; @@ -69,4 +111,7 @@ fn main() { let _ = NonExhaustiveStruct::default(); let _ = SomeEnum::default(); let _ = NonDefaultStruct::default(); + let _ = EmptyStruct::default(); + let _ = FakeDefault::default(); + let _ = ::default(); } diff --git a/tests/ui/default_constructed_unit_structs.stderr b/tests/ui/default_constructed_unit_structs.stderr index 23ffa278288..fa39ef4cda1 100644 --- a/tests/ui/default_constructed_unit_structs.stderr +++ b/tests/ui/default_constructed_unit_structs.stderr @@ -1,28 +1,34 @@ error: use of `default` to create a unit struct - --> $DIR/default_constructed_unit_structs.rs:39:31 + --> $DIR/default_constructed_unit_structs.rs:11:13 | -LL | inner: PhantomData::default(), - | ^^^^^^^^^^^ help: remove this call to `default` +LL | Self::default() + | ^^^^^^^^^^^ help: remove this call to `default` | = note: `-D clippy::default-constructed-unit-structs` implied by `-D warnings` error: use of `default` to create a unit struct - --> $DIR/default_constructed_unit_structs.rs:62:33 + --> $DIR/default_constructed_unit_structs.rs:53:31 + | +LL | inner: PhantomData::default(), + | ^^^^^^^^^^^ help: remove this call to `default` + +error: use of `default` to create a unit struct + --> $DIR/default_constructed_unit_structs.rs:104:33 | LL | let _ = PhantomData::::default(); | ^^^^^^^^^^^ help: remove this call to `default` error: use of `default` to create a unit struct - --> $DIR/default_constructed_unit_structs.rs:63:42 + --> $DIR/default_constructed_unit_structs.rs:105:42 | LL | let _: PhantomData = PhantomData::default(); | ^^^^^^^^^^^ help: remove this call to `default` error: use of `default` to create a unit struct - --> $DIR/default_constructed_unit_structs.rs:64:23 + --> $DIR/default_constructed_unit_structs.rs:106:23 | LL | let _ = UnitStruct::default(); | ^^^^^^^^^^^ help: remove this call to `default` -error: aborting due to 4 previous errors +error: aborting due to 5 previous errors