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