Skip to content

Commit 8701009

Browse files
committed
add more test cases
1 parent 4e04903 commit 8701009

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

clippy_lints/src/default_constructed_unit_structs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs {
4242
if_chain!(
4343
// make sure we have a call to `Default::default`
4444
if let hir::ExprKind::Call(fn_expr, &[]) = expr.kind;
45-
if let ExprKind::Path(ref qpath) = fn_expr.kind;
45+
if let ExprKind::Path(ref qpath@ hir::QPath::TypeRelative(_,_)) = fn_expr.kind;
4646
if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id);
4747
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
4848
// make sure we have a struct with no fields (unit struct)
4949
if let ty::Adt(def, ..) = cx.typeck_results().expr_ty(expr).kind();
50-
if def.is_struct() && def.is_payloadfree()
51-
&& !def.non_enum_variant().is_field_list_non_exhaustive()
52-
&& !is_from_proc_macro(cx, expr);
50+
if def.is_struct();
51+
if let var @ ty::VariantDef { ctor: Some((hir::def::CtorKind::Const, _)), .. } = def.non_enum_variant();
52+
if !var.is_field_list_non_exhaustive() && !is_from_proc_macro(cx, expr);
5353
then {
5454
span_lint_and_sugg(
5555
cx,

tests/ui/default_constructed_unit_structs.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@ use std::marker::PhantomData;
55
#[derive(Default)]
66
struct UnitStruct;
77

8+
impl UnitStruct {
9+
fn new() -> Self {
10+
//should lint
11+
Self::default()
12+
}
13+
}
14+
815
#[derive(Default)]
916
struct TupleStruct(usize);
1017

18+
impl TupleStruct {
19+
fn new() -> Self {
20+
// should not lint
21+
Self(Default::default())
22+
}
23+
}
24+
1125
// no lint for derived impl
1226
#[derive(Default)]
1327
struct NormalStruct {
@@ -39,6 +53,13 @@ impl NormalStruct {
3953
inner: PhantomData::default(),
4054
}
4155
}
56+
57+
fn new2() -> Self {
58+
// should not lint
59+
Self {
60+
inner: Default::default(),
61+
}
62+
}
4263
}
4364

4465
#[derive(Default)]
@@ -51,8 +72,29 @@ impl<T: Default> GenericStruct<T> {
5172
// should not lint
5273
Self { t: T::default() }
5374
}
75+
76+
fn new2() -> Self {
77+
// should not lint
78+
Self { t: Default::default() }
79+
}
80+
}
81+
82+
struct FakeDefault;
83+
impl FakeDefault {
84+
fn default() -> Self {
85+
Self
86+
}
5487
}
5588

89+
impl Default for FakeDefault {
90+
fn default() -> Self {
91+
Self
92+
}
93+
}
94+
95+
#[derive(Default)]
96+
struct EmptyStruct {}
97+
5698
#[derive(Default)]
5799
#[non_exhaustive]
58100
struct NonExhaustiveStruct;
@@ -69,4 +111,7 @@ fn main() {
69111
let _ = NonExhaustiveStruct::default();
70112
let _ = SomeEnum::default();
71113
let _ = NonDefaultStruct::default();
114+
let _ = EmptyStruct::default();
115+
let _ = FakeDefault::default();
116+
let _ = <FakeDefault as Default>::default();
72117
}
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
error: use of `default` to create a unit struct
2-
--> $DIR/default_constructed_unit_structs.rs:39:31
2+
--> $DIR/default_constructed_unit_structs.rs:11:13
33
|
4-
LL | inner: PhantomData::default(),
5-
| ^^^^^^^^^^^ help: remove this call to `default`
4+
LL | Self::default()
5+
| ^^^^^^^^^^^ help: remove this call to `default`
66
|
77
= note: `-D clippy::default-constructed-unit-structs` implied by `-D warnings`
88

99
error: use of `default` to create a unit struct
10-
--> $DIR/default_constructed_unit_structs.rs:62:33
10+
--> $DIR/default_constructed_unit_structs.rs:53:31
11+
|
12+
LL | inner: PhantomData::default(),
13+
| ^^^^^^^^^^^ help: remove this call to `default`
14+
15+
error: use of `default` to create a unit struct
16+
--> $DIR/default_constructed_unit_structs.rs:104:33
1117
|
1218
LL | let _ = PhantomData::<usize>::default();
1319
| ^^^^^^^^^^^ help: remove this call to `default`
1420

1521
error: use of `default` to create a unit struct
16-
--> $DIR/default_constructed_unit_structs.rs:63:42
22+
--> $DIR/default_constructed_unit_structs.rs:105:42
1723
|
1824
LL | let _: PhantomData<i32> = PhantomData::default();
1925
| ^^^^^^^^^^^ help: remove this call to `default`
2026

2127
error: use of `default` to create a unit struct
22-
--> $DIR/default_constructed_unit_structs.rs:64:23
28+
--> $DIR/default_constructed_unit_structs.rs:106:23
2329
|
2430
LL | let _ = UnitStruct::default();
2531
| ^^^^^^^^^^^ help: remove this call to `default`
2632

27-
error: aborting due to 4 previous errors
33+
error: aborting due to 5 previous errors
2834

0 commit comments

Comments
 (0)