]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/derivable_impls.fixed
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / derivable_impls.fixed
1 #![allow(dead_code)]
2
3 use std::collections::HashMap;
4
5 #[derive(Default)]
6 struct FooDefault<'a> {
7 a: bool,
8 b: i32,
9 c: u64,
10 d: Vec<i32>,
11 e: FooND1,
12 f: FooND2,
13 g: HashMap<i32, i32>,
14 h: (i32, Vec<i32>),
15 i: [Vec<i32>; 3],
16 j: [i32; 5],
17 k: Option<i32>,
18 l: &'a [i32],
19 }
20
21
22
23 #[derive(Default)]
24 struct TupleDefault(bool, i32, u64);
25
26
27
28 struct FooND1 {
29 a: bool,
30 }
31
32 impl std::default::Default for FooND1 {
33 fn default() -> Self {
34 Self { a: true }
35 }
36 }
37
38 struct FooND2 {
39 a: i32,
40 }
41
42 impl std::default::Default for FooND2 {
43 fn default() -> Self {
44 Self { a: 5 }
45 }
46 }
47
48 struct FooNDNew {
49 a: bool,
50 }
51
52 impl FooNDNew {
53 fn new() -> Self {
54 Self { a: true }
55 }
56 }
57
58 impl Default for FooNDNew {
59 fn default() -> Self {
60 Self::new()
61 }
62 }
63
64 struct FooNDVec(Vec<i32>);
65
66 impl Default for FooNDVec {
67 fn default() -> Self {
68 Self(vec![5, 12])
69 }
70 }
71
72 #[derive(Default)]
73 struct StrDefault<'a>(&'a str);
74
75
76
77 #[derive(Default)]
78 struct AlreadyDerived(i32, bool);
79
80 macro_rules! mac {
81 () => {
82 0
83 };
84 ($e:expr) => {
85 struct X(u32);
86 impl Default for X {
87 fn default() -> Self {
88 Self($e)
89 }
90 }
91 };
92 }
93
94 mac!(0);
95
96 #[derive(Default)]
97 struct Y(u32);
98
99
100 struct RustIssue26925<T> {
101 a: Option<T>,
102 }
103
104 // We should watch out for cases where a manual impl is needed because a
105 // derive adds different type bounds (https://github.com/rust-lang/rust/issues/26925).
106 // For example, a struct with Option<T> does not require T: Default, but a derive adds
107 // that type bound anyways. So until #26925 get fixed we should disable lint
108 // for the following case
109 impl<T> Default for RustIssue26925<T> {
110 fn default() -> Self {
111 Self { a: None }
112 }
113 }
114
115 struct SpecializedImpl<A, B> {
116 a: A,
117 b: B,
118 }
119
120 impl<T: Default> Default for SpecializedImpl<T, T> {
121 fn default() -> Self {
122 Self {
123 a: T::default(),
124 b: T::default(),
125 }
126 }
127 }
128
129 #[derive(Default)]
130 struct WithoutSelfCurly {
131 a: bool,
132 }
133
134
135
136 #[derive(Default)]
137 struct WithoutSelfParan(bool);
138
139
140
141 // https://github.com/rust-lang/rust-clippy/issues/7655
142
143 pub struct SpecializedImpl2<T> {
144 v: Vec<T>,
145 }
146
147 impl Default for SpecializedImpl2<String> {
148 fn default() -> Self {
149 Self { v: Vec::new() }
150 }
151 }
152
153 // https://github.com/rust-lang/rust-clippy/issues/7654
154
155 pub struct Color {
156 pub r: u8,
157 pub g: u8,
158 pub b: u8,
159 }
160
161 /// `#000000`
162 impl Default for Color {
163 fn default() -> Self {
164 Color { r: 0, g: 0, b: 0 }
165 }
166 }
167
168 pub struct Color2 {
169 pub r: u8,
170 pub g: u8,
171 pub b: u8,
172 }
173
174 impl Default for Color2 {
175 /// `#000000`
176 fn default() -> Self {
177 Self { r: 0, g: 0, b: 0 }
178 }
179 }
180
181 #[derive(Default)]
182 pub struct RepeatDefault1 {
183 a: [i8; 32],
184 }
185
186
187
188 pub struct RepeatDefault2 {
189 a: [i8; 33],
190 }
191
192 impl Default for RepeatDefault2 {
193 fn default() -> Self {
194 RepeatDefault2 { a: [0; 33] }
195 }
196 }
197
198 // https://github.com/rust-lang/rust-clippy/issues/7753
199
200 pub enum IntOrString {
201 Int(i32),
202 String(String),
203 }
204
205 impl Default for IntOrString {
206 fn default() -> Self {
207 IntOrString::Int(0)
208 }
209 }
210
211 #[derive(Default)]
212 pub enum SimpleEnum {
213 Foo,
214 #[default]
215 Bar,
216 }
217
218
219
220 pub enum NonExhaustiveEnum {
221 Foo,
222 #[non_exhaustive]
223 Bar,
224 }
225
226 impl Default for NonExhaustiveEnum {
227 fn default() -> Self {
228 NonExhaustiveEnum::Bar
229 }
230 }
231
232 // https://github.com/rust-lang/rust-clippy/issues/10396
233
234 #[derive(Default)]
235 struct DefaultType;
236
237 struct GenericType<T = DefaultType> {
238 t: T,
239 }
240
241 impl Default for GenericType {
242 fn default() -> Self {
243 Self { t: Default::default() }
244 }
245 }
246
247 struct InnerGenericType<T> {
248 t: T,
249 }
250
251 impl Default for InnerGenericType<DefaultType> {
252 fn default() -> Self {
253 Self { t: Default::default() }
254 }
255 }
256
257 struct OtherGenericType<T = DefaultType> {
258 inner: InnerGenericType<T>,
259 }
260
261 impl Default for OtherGenericType {
262 fn default() -> Self {
263 Self {
264 inner: Default::default(),
265 }
266 }
267 }
268
269 mod issue10158 {
270 pub trait T {}
271
272 #[derive(Default)]
273 pub struct S {}
274 impl T for S {}
275
276 pub struct Outer {
277 pub inner: Box<dyn T>,
278 }
279
280 impl Default for Outer {
281 fn default() -> Self {
282 Outer {
283 // Box::<S>::default() adjusts to Box<dyn T>
284 inner: Box::<S>::default(),
285 }
286 }
287 }
288 }
289
290 mod issue11368 {
291 pub struct A {
292 a: u32,
293 }
294
295 impl Default for A {
296 #[track_caller]
297 fn default() -> Self {
298 Self { a: 0 }
299 }
300 }
301 }
302
303 fn main() {}