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