summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/field_reassign_with_default.rs
blob: 7367910eaa126fdc54319aa3d7232765dcca376d (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
// aux-build:proc_macro_derive.rs
// aux-build:macro_rules.rs

#![warn(clippy::field_reassign_with_default)]

#[macro_use]
extern crate proc_macro_derive;
#[macro_use]
extern crate macro_rules;

// Don't lint on derives that derive `Default`
// See https://github.com/rust-lang/rust-clippy/issues/6545
#[derive(FieldReassignWithDefault)]
struct DerivedStruct;

#[derive(Default)]
struct A {
    i: i32,
    j: i64,
}

struct B {
    i: i32,
    j: i64,
}

#[derive(Default)]
struct C {
    i: Vec<i32>,
    j: i64,
}

#[derive(Default)]
struct D {
    a: Option<i32>,
    b: Option<i32>,
}

macro_rules! m {
    ($key:ident: $value:tt) => {{
        let mut data = $crate::D::default();
        data.$key = Some($value);
        data
    }};
}

/// Implements .next() that returns a different number each time.
struct SideEffect(i32);

impl SideEffect {
    fn new() -> SideEffect {
        SideEffect(0)
    }
    fn next(&mut self) -> i32 {
        self.0 += 1;
        self.0
    }
}

fn main() {
    // wrong, produces first error in stderr
    let mut a: A = Default::default();
    a.i = 42;

    // right
    let mut a: A = Default::default();

    // right
    let a = A {
        i: 42,
        ..Default::default()
    };

    // right
    let mut a: A = Default::default();
    if a.i == 0 {
        a.j = 12;
    }

    // right
    let mut a: A = Default::default();
    let b = 5;

    // right
    let mut b = 32;
    let mut a: A = Default::default();
    b = 2;

    // right
    let b: B = B { i: 42, j: 24 };

    // right
    let mut b: B = B { i: 42, j: 24 };
    b.i = 52;

    // right
    let mut b = B { i: 15, j: 16 };
    let mut a: A = Default::default();
    b.i = 2;

    // wrong, produces second error in stderr
    let mut a: A = Default::default();
    a.j = 43;
    a.i = 42;

    // wrong, produces third error in stderr
    let mut a: A = Default::default();
    a.i = 42;
    a.j = 43;
    a.j = 44;

    // wrong, produces fourth error in stderr
    let mut a = A::default();
    a.i = 42;

    // wrong, but does not produce an error in stderr, because we can't produce a correct kind of
    // suggestion with current implementation
    let mut c: (i32, i32) = Default::default();
    c.0 = 42;
    c.1 = 21;

    // wrong, produces the fifth error in stderr
    let mut a: A = Default::default();
    a.i = Default::default();

    // wrong, produces the sixth error in stderr
    let mut a: A = Default::default();
    a.i = Default::default();
    a.j = 45;

    // right, because an assignment refers to another field
    let mut x = A::default();
    x.i = 42;
    x.j = 21 + x.i as i64;

    // right, we bail out if there's a reassignment to the same variable, since there is a risk of
    // side-effects affecting the outcome
    let mut x = A::default();
    let mut side_effect = SideEffect::new();
    x.i = side_effect.next();
    x.j = 2;
    x.i = side_effect.next();

    // don't lint - some private fields
    let mut x = m::F::default();
    x.a = 1;

    // don't expand macros in the suggestion (#6522)
    let mut a: C = C::default();
    a.i = vec![1];

    // Don't lint in external macros
    field_reassign_with_default!();

    // be sure suggestion is correct with generics
    let mut a: Wrapper<bool> = Default::default();
    a.i = true;

    let mut a: WrapperMulti<i32, i64> = Default::default();
    a.i = 42;

    // Don't lint in macros
    m! {
        a: 42
    };
}

mod m {
    #[derive(Default)]
    pub struct F {
        pub a: u64,
        b: u64,
    }
}

#[derive(Default)]
struct Wrapper<T> {
    i: T,
}

#[derive(Default)]
struct WrapperMulti<T, U> {
    i: T,
    j: U,
}

mod issue6312 {
    use std::sync::atomic::AtomicBool;
    use std::sync::Arc;

    // do not lint: type implements `Drop` but not all fields are `Copy`
    #[derive(Clone, Default)]
    pub struct ImplDropNotAllCopy {
        name: String,
        delay_data_sync: Arc<AtomicBool>,
    }

    impl Drop for ImplDropNotAllCopy {
        fn drop(&mut self) {
            self.close()
        }
    }

    impl ImplDropNotAllCopy {
        fn new(name: &str) -> Self {
            let mut f = ImplDropNotAllCopy::default();
            f.name = name.to_owned();
            f
        }
        fn close(&self) {}
    }

    // lint: type implements `Drop` and all fields are `Copy`
    #[derive(Clone, Default)]
    pub struct ImplDropAllCopy {
        name: usize,
        delay_data_sync: bool,
    }

    impl Drop for ImplDropAllCopy {
        fn drop(&mut self) {
            self.close()
        }
    }

    impl ImplDropAllCopy {
        fn new(name: &str) -> Self {
            let mut f = ImplDropAllCopy::default();
            f.name = name.len();
            f
        }
        fn close(&self) {}
    }

    // lint: type does not implement `Drop` though all fields are `Copy`
    #[derive(Clone, Default)]
    pub struct NoDropAllCopy {
        name: usize,
        delay_data_sync: bool,
    }

    impl NoDropAllCopy {
        fn new(name: &str) -> Self {
            let mut f = NoDropAllCopy::default();
            f.name = name.len();
            f
        }
    }
}