1 use std::collections::HashMap;
2
3 struct FooDefault<'a> {
4 a: bool,
5 b: i32,
6 c: u64,
7 d: Vec<i32>,
8 e: FooND1,
9 f: FooND2,
10 g: HashMap<i32, i32>,
11 h: (i32, Vec<i32>),
12 i: [Vec<i32>; 3],
13 j: [i32; 5],
14 k: Option<i32>,
15 l: &'a [i32],
16 }
17
18 impl std::default::Default for FooDefault<'_> {
default() -> Self19 fn default() -> Self {
20 Self {
21 a: false,
22 b: 0,
23 c: 0u64,
24 d: vec![],
25 e: Default::default(),
26 f: FooND2::default(),
27 g: HashMap::new(),
28 h: (0, vec![]),
29 i: [vec![], vec![], vec![]],
30 j: [0; 5],
31 k: None,
32 l: &[],
33 }
34 }
35 }
36
37 struct TupleDefault(bool, i32, u64);
38
39 impl std::default::Default for TupleDefault {
default() -> Self40 fn default() -> Self {
41 Self(false, 0, 0u64)
42 }
43 }
44
45 struct FooND1 {
46 a: bool,
47 }
48
49 impl std::default::Default for FooND1 {
default() -> Self50 fn default() -> Self {
51 Self { a: true }
52 }
53 }
54
55 struct FooND2 {
56 a: i32,
57 }
58
59 impl std::default::Default for FooND2 {
default() -> Self60 fn default() -> Self {
61 Self { a: 5 }
62 }
63 }
64
65 struct FooNDNew {
66 a: bool,
67 }
68
69 impl FooNDNew {
new() -> Self70 fn new() -> Self {
71 Self { a: true }
72 }
73 }
74
75 impl Default for FooNDNew {
default() -> Self76 fn default() -> Self {
77 Self::new()
78 }
79 }
80
81 struct FooNDVec(Vec<i32>);
82
83 impl Default for FooNDVec {
default() -> Self84 fn default() -> Self {
85 Self(vec![5, 12])
86 }
87 }
88
89 struct StrDefault<'a>(&'a str);
90
91 impl Default for StrDefault<'_> {
default() -> Self92 fn default() -> Self {
93 Self("")
94 }
95 }
96
97 #[derive(Default)]
98 struct AlreadyDerived(i32, bool);
99
100 macro_rules! mac {
101 () => {
102 0
103 };
104 ($e:expr) => {
105 struct X(u32);
106 impl Default for X {
107 fn default() -> Self {
108 Self($e)
109 }
110 }
111 };
112 }
113
114 mac!(0);
115
116 struct Y(u32);
117 impl Default for Y {
default() -> Self118 fn default() -> Self {
119 Self(mac!())
120 }
121 }
122
123 struct RustIssue26925<T> {
124 a: Option<T>,
125 }
126
127 // We should watch out for cases where a manual impl is needed because a
128 // derive adds different type bounds (https://github.com/rust-lang/rust/issues/26925).
129 // For example, a struct with Option<T> does not require T: Default, but a derive adds
130 // that type bound anyways. So until #26925 get fixed we should disable lint
131 // for the following case
132 impl<T> Default for RustIssue26925<T> {
default() -> Self133 fn default() -> Self {
134 Self { a: None }
135 }
136 }
137
138 struct SpecializedImpl<A, B> {
139 a: A,
140 b: B,
141 }
142
143 impl<T: Default> Default for SpecializedImpl<T, T> {
default() -> Self144 fn default() -> Self {
145 Self {
146 a: T::default(),
147 b: T::default(),
148 }
149 }
150 }
151
152 struct WithoutSelfCurly {
153 a: bool,
154 }
155
156 impl Default for WithoutSelfCurly {
default() -> Self157 fn default() -> Self {
158 WithoutSelfCurly { a: false }
159 }
160 }
161
162 struct WithoutSelfParan(bool);
163
164 impl Default for WithoutSelfParan {
default() -> Self165 fn default() -> Self {
166 WithoutSelfParan(false)
167 }
168 }
169
170 // https://github.com/rust-lang/rust-clippy/issues/7655
171
172 pub struct SpecializedImpl2<T> {
173 v: Vec<T>,
174 }
175
176 impl Default for SpecializedImpl2<String> {
default() -> Self177 fn default() -> Self {
178 Self { v: Vec::new() }
179 }
180 }
181
182 // https://github.com/rust-lang/rust-clippy/issues/7654
183
184 pub struct Color {
185 pub r: u8,
186 pub g: u8,
187 pub b: u8,
188 }
189
190 /// `#000000`
191 impl Default for Color {
default() -> Self192 fn default() -> Self {
193 Color { r: 0, g: 0, b: 0 }
194 }
195 }
196
197 pub struct Color2 {
198 pub r: u8,
199 pub g: u8,
200 pub b: u8,
201 }
202
203 impl Default for Color2 {
204 /// `#000000`
default() -> Self205 fn default() -> Self {
206 Self { r: 0, g: 0, b: 0 }
207 }
208 }
209
210 pub struct RepeatDefault1 {
211 a: [i8; 32],
212 }
213
214 impl Default for RepeatDefault1 {
default() -> Self215 fn default() -> Self {
216 RepeatDefault1 { a: [0; 32] }
217 }
218 }
219
220 pub struct RepeatDefault2 {
221 a: [i8; 33],
222 }
223
224 impl Default for RepeatDefault2 {
default() -> Self225 fn default() -> Self {
226 RepeatDefault2 { a: [0; 33] }
227 }
228 }
229
230 // https://github.com/rust-lang/rust-clippy/issues/7753
231
232 pub enum IntOrString {
233 Int(i32),
234 String(String),
235 }
236
237 impl Default for IntOrString {
default() -> Self238 fn default() -> Self {
239 IntOrString::Int(0)
240 }
241 }
242
main()243 fn main() {}
244