summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/derivable_impls.rs
blob: 18cef1c5be895336151eac54156c5ba5ecf05ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//@run-rustfix

#![allow(dead_code)]

use std::collections::HashMap;

struct FooDefault<'a> {
    a: bool,
    b: i32,
    c: u64,
    d: Vec<i32>,
    e: FooND1,
    f: FooND2,
    g: HashMap<i32, i32>,
    h: (i32, Vec<i32>),
    i: [Vec<i32>; 3],
    j: [i32; 5],
    k: Option<i32>,
    l: &'a [i32],
}

impl std::default::Default for FooDefault<'_> {
    fn default() -> Self {
        Self {
            a: false,
            b: 0,
            c: 0u64,
            d: vec![],
            e: Default::default(),
            f: FooND2::default(),
            g: HashMap::new(),
            h: (0, vec![]),
            i: [vec![], vec![], vec![]],
            j: [0; 5],
            k: None,
            l: &[],
        }
    }
}

struct TupleDefault(bool, i32, u64);

impl std::default::Default for TupleDefault {
    fn default() -> Self {
        Self(false, 0, 0u64)
    }
}

struct FooND1 {
    a: bool,
}

impl std::default::Default for FooND1 {
    fn default() -> Self {
        Self { a: true }
    }
}

struct FooND2 {
    a: i32,
}

impl std::default::Default for FooND2 {
    fn default() -> Self {
        Self { a: 5 }
    }
}

struct FooNDNew {
    a: bool,
}

impl FooNDNew {
    fn new() -> Self {
        Self { a: true }
    }
}

impl Default for FooNDNew {
    fn default() -> Self {
        Self::new()
    }
}

struct FooNDVec(Vec<i32>);

impl Default for FooNDVec {
    fn default() -> Self {
        Self(vec![5, 12])
    }
}

struct StrDefault<'a>(&'a str);

impl Default for StrDefault<'_> {
    fn default() -> Self {
        Self("")
    }
}

#[derive(Default)]
struct AlreadyDerived(i32, bool);

macro_rules! mac {
    () => {
        0
    };
    ($e:expr) => {
        struct X(u32);
        impl Default for X {
            fn default() -> Self {
                Self($e)
            }
        }
    };
}

mac!(0);

struct Y(u32);
impl Default for Y {
    fn default() -> Self {
        Self(mac!())
    }
}

struct RustIssue26925<T> {
    a: Option<T>,
}

// We should watch out for cases where a manual impl is needed because a
// derive adds different type bounds (https://github.com/rust-lang/rust/issues/26925).
// For example, a struct with Option<T> does not require T: Default, but a derive adds
// that type bound anyways. So until #26925 get fixed we should disable lint
// for the following case
impl<T> Default for RustIssue26925<T> {
    fn default() -> Self {
        Self { a: None }
    }
}

struct SpecializedImpl<A, B> {
    a: A,
    b: B,
}

impl<T: Default> Default for SpecializedImpl<T, T> {
    fn default() -> Self {
        Self {
            a: T::default(),
            b: T::default(),
        }
    }
}

struct WithoutSelfCurly {
    a: bool,
}

impl Default for WithoutSelfCurly {
    fn default() -> Self {
        WithoutSelfCurly { a: false }
    }
}

struct WithoutSelfParan(bool);

impl Default for WithoutSelfParan {
    fn default() -> Self {
        WithoutSelfParan(false)
    }
}

// https://github.com/rust-lang/rust-clippy/issues/7655

pub struct SpecializedImpl2<T> {
    v: Vec<T>,
}

impl Default for SpecializedImpl2<String> {
    fn default() -> Self {
        Self { v: Vec::new() }
    }
}

// https://github.com/rust-lang/rust-clippy/issues/7654

pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

/// `#000000`
impl Default for Color {
    fn default() -> Self {
        Color { r: 0, g: 0, b: 0 }
    }
}

pub struct Color2 {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

impl Default for Color2 {
    /// `#000000`
    fn default() -> Self {
        Self { r: 0, g: 0, b: 0 }
    }
}

pub struct RepeatDefault1 {
    a: [i8; 32],
}

impl Default for RepeatDefault1 {
    fn default() -> Self {
        RepeatDefault1 { a: [0; 32] }
    }
}

pub struct RepeatDefault2 {
    a: [i8; 33],
}

impl Default for RepeatDefault2 {
    fn default() -> Self {
        RepeatDefault2 { a: [0; 33] }
    }
}

// https://github.com/rust-lang/rust-clippy/issues/7753

pub enum IntOrString {
    Int(i32),
    String(String),
}

impl Default for IntOrString {
    fn default() -> Self {
        IntOrString::Int(0)
    }
}

pub enum SimpleEnum {
    Foo,
    Bar,
}

impl Default for SimpleEnum {
    fn default() -> Self {
        SimpleEnum::Bar
    }
}

pub enum NonExhaustiveEnum {
    Foo,
    #[non_exhaustive]
    Bar,
}

impl Default for NonExhaustiveEnum {
    fn default() -> Self {
        NonExhaustiveEnum::Bar
    }
}

// https://github.com/rust-lang/rust-clippy/issues/10396

#[derive(Default)]
struct DefaultType;

struct GenericType<T = DefaultType> {
    t: T,
}

impl Default for GenericType {
    fn default() -> Self {
        Self { t: Default::default() }
    }
}

struct InnerGenericType<T> {
    t: T,
}

impl Default for InnerGenericType<DefaultType> {
    fn default() -> Self {
        Self { t: Default::default() }
    }
}

struct OtherGenericType<T = DefaultType> {
    inner: InnerGenericType<T>,
}

impl Default for OtherGenericType {
    fn default() -> Self {
        Self {
            inner: Default::default(),
        }
    }
}

mod issue10158 {
    pub trait T {}

    #[derive(Default)]
    pub struct S {}
    impl T for S {}

    pub struct Outer {
        pub inner: Box<dyn T>,
    }

    impl Default for Outer {
        fn default() -> Self {
            Outer {
                // Box::<S>::default() adjusts to Box<dyn T>
                inner: Box::<S>::default(),
            }
        }
    }
}

fn main() {}