Skip to content

Commit 1603715

Browse files
committed
add rustfix annotation
1 parent 4ed7fd1 commit 1603715

3 files changed

+126
-5
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//@run-rustfix
2+
3+
#![allow(unused)]
4+
#![warn(clippy::default_constructed_unit_structs)]
5+
use std::marker::PhantomData;
6+
7+
#[derive(Default)]
8+
struct UnitStruct;
9+
10+
impl UnitStruct {
11+
fn new() -> Self {
12+
//should lint
13+
Self
14+
}
15+
}
16+
17+
#[derive(Default)]
18+
struct TupleStruct(usize);
19+
20+
impl TupleStruct {
21+
fn new() -> Self {
22+
// should not lint
23+
Self(Default::default())
24+
}
25+
}
26+
27+
// no lint for derived impl
28+
#[derive(Default)]
29+
struct NormalStruct {
30+
inner: PhantomData<usize>,
31+
}
32+
33+
struct NonDefaultStruct;
34+
35+
impl NonDefaultStruct {
36+
fn default() -> Self {
37+
Self
38+
}
39+
}
40+
41+
#[derive(Default)]
42+
enum SomeEnum {
43+
#[default]
44+
Unit,
45+
Tuple(UnitStruct),
46+
Struct {
47+
inner: usize,
48+
},
49+
}
50+
51+
impl NormalStruct {
52+
fn new() -> Self {
53+
// should lint
54+
Self {
55+
inner: PhantomData,
56+
}
57+
}
58+
59+
fn new2() -> Self {
60+
// should not lint
61+
Self {
62+
inner: Default::default(),
63+
}
64+
}
65+
}
66+
67+
#[derive(Default)]
68+
struct GenericStruct<T> {
69+
t: T,
70+
}
71+
72+
impl<T: Default> GenericStruct<T> {
73+
fn new() -> Self {
74+
// should not lint
75+
Self { t: T::default() }
76+
}
77+
78+
fn new2() -> Self {
79+
// should not lint
80+
Self { t: Default::default() }
81+
}
82+
}
83+
84+
struct FakeDefault;
85+
impl FakeDefault {
86+
fn default() -> Self {
87+
Self
88+
}
89+
}
90+
91+
impl Default for FakeDefault {
92+
fn default() -> Self {
93+
Self
94+
}
95+
}
96+
97+
#[derive(Default)]
98+
struct EmptyStruct {}
99+
100+
#[derive(Default)]
101+
#[non_exhaustive]
102+
struct NonExhaustiveStruct;
103+
104+
fn main() {
105+
// should lint
106+
let _ = PhantomData::<usize>;
107+
let _: PhantomData<i32> = PhantomData;
108+
let _ = UnitStruct;
109+
110+
// should not lint
111+
let _ = TupleStruct::default();
112+
let _ = NormalStruct::default();
113+
let _ = NonExhaustiveStruct::default();
114+
let _ = SomeEnum::default();
115+
let _ = NonDefaultStruct::default();
116+
let _ = EmptyStruct::default();
117+
let _ = FakeDefault::default();
118+
let _ = <FakeDefault as Default>::default();
119+
}

tests/ui/default_constructed_unit_structs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@run-rustfix
2+
13
#![allow(unused)]
24
#![warn(clippy::default_constructed_unit_structs)]
35
use std::marker::PhantomData;

tests/ui/default_constructed_unit_structs.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: use of `default` to create a unit struct
2-
--> $DIR/default_constructed_unit_structs.rs:11:13
2+
--> $DIR/default_constructed_unit_structs.rs:13:13
33
|
44
LL | Self::default()
55
| ^^^^^^^^^^^ 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:53:31
10+
--> $DIR/default_constructed_unit_structs.rs:55:31
1111
|
1212
LL | inner: PhantomData::default(),
1313
| ^^^^^^^^^^^ help: remove this call to `default`
1414

1515
error: use of `default` to create a unit struct
16-
--> $DIR/default_constructed_unit_structs.rs:104:33
16+
--> $DIR/default_constructed_unit_structs.rs:106:33
1717
|
1818
LL | let _ = PhantomData::<usize>::default();
1919
| ^^^^^^^^^^^ help: remove this call to `default`
2020

2121
error: use of `default` to create a unit struct
22-
--> $DIR/default_constructed_unit_structs.rs:105:42
22+
--> $DIR/default_constructed_unit_structs.rs:107:42
2323
|
2424
LL | let _: PhantomData<i32> = PhantomData::default();
2525
| ^^^^^^^^^^^ help: remove this call to `default`
2626

2727
error: use of `default` to create a unit struct
28-
--> $DIR/default_constructed_unit_structs.rs:106:23
28+
--> $DIR/default_constructed_unit_structs.rs:108:23
2929
|
3030
LL | let _ = UnitStruct::default();
3131
| ^^^^^^^^^^^ help: remove this call to `default`

0 commit comments

Comments
 (0)