fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
4.6 KiB303 linesraw
1#![allow(dead_code)]
2
3use std::collections::HashMap;
4
5#[derive(Default)]
6struct 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)]
24struct TupleDefault(bool, i32, u64);
25
26
27
28struct FooND1 {
29    a: bool,
30}
31
32impl std::default::Default for FooND1 {
33    fn default() -> Self {
34        Self { a: true }
35    }
36}
37
38struct FooND2 {
39    a: i32,
40}
41
42impl std::default::Default for FooND2 {
43    fn default() -> Self {
44        Self { a: 5 }
45    }
46}
47
48struct FooNDNew {
49    a: bool,
50}
51
52impl FooNDNew {
53    fn new() -> Self {
54        Self { a: true }
55    }
56}
57
58impl Default for FooNDNew {
59    fn default() -> Self {
60        Self::new()
61    }
62}
63
64struct FooNDVec(Vec<i32>);
65
66impl Default for FooNDVec {
67    fn default() -> Self {
68        Self(vec![5, 12])
69    }
70}
71
72#[derive(Default)]
73struct StrDefault<'a>(&'a str);
74
75
76
77#[derive(Default)]
78struct AlreadyDerived(i32, bool);
79
80macro_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
94mac!(0);
95
96#[derive(Default)]
97struct Y(u32);
98
99
100struct 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
109impl<T> Default for RustIssue26925<T> {
110    fn default() -> Self {
111        Self { a: None }
112    }
113}
114
115struct SpecializedImpl<A, B> {
116    a: A,
117    b: B,
118}
119
120impl<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)]
130struct WithoutSelfCurly {
131    a: bool,
132}
133
134
135
136#[derive(Default)]
137struct WithoutSelfParan(bool);
138
139
140
141// https://github.com/rust-lang/rust-clippy/issues/7655
142
143pub struct SpecializedImpl2<T> {
144    v: Vec<T>,
145}
146
147impl 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
155pub struct Color {
156    pub r: u8,
157    pub g: u8,
158    pub b: u8,
159}
160
161/// `#000000`
162impl Default for Color {
163    fn default() -> Self {
164        Color { r: 0, g: 0, b: 0 }
165    }
166}
167
168pub struct Color2 {
169    pub r: u8,
170    pub g: u8,
171    pub b: u8,
172}
173
174impl Default for Color2 {
175    /// `#000000`
176    fn default() -> Self {
177        Self { r: 0, g: 0, b: 0 }
178    }
179}
180
181#[derive(Default)]
182pub struct RepeatDefault1 {
183    a: [i8; 32],
184}
185
186
187
188pub struct RepeatDefault2 {
189    a: [i8; 33],
190}
191
192impl 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
200pub enum IntOrString {
201    Int(i32),
202    String(String),
203}
204
205impl Default for IntOrString {
206    fn default() -> Self {
207        IntOrString::Int(0)
208    }
209}
210
211#[derive(Default)]
212pub enum SimpleEnum {
213    Foo,
214    #[default]
215    Bar,
216}
217
218
219
220pub enum NonExhaustiveEnum {
221    Foo,
222    #[non_exhaustive]
223    Bar,
224}
225
226impl 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)]
235struct DefaultType;
236
237struct GenericType<T = DefaultType> {
238    t: T,
239}
240
241impl Default for GenericType {
242    fn default() -> Self {
243        Self { t: Default::default() }
244    }
245}
246
247struct InnerGenericType<T> {
248    t: T,
249}
250
251impl Default for InnerGenericType<DefaultType> {
252    fn default() -> Self {
253        Self { t: Default::default() }
254    }
255}
256
257struct OtherGenericType<T = DefaultType> {
258    inner: InnerGenericType<T>,
259}
260
261impl Default for OtherGenericType {
262    fn default() -> Self {
263        Self {
264            inner: Default::default(),
265        }
266    }
267}
268
269mod 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
290mod 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
303fn main() {}