]> git.lizzy.rs Git - rust.git/blob - tests/ui/field_reassign_with_default.rs
Improved shared_code_in_if_blocks message and added test stderrs
[rust.git] / tests / ui / field_reassign_with_default.rs
1 // aux-build:proc_macro_derive.rs
2 // aux-build:macro_rules.rs
3
4 #![warn(clippy::field_reassign_with_default)]
5
6 #[macro_use]
7 extern crate proc_macro_derive;
8 #[macro_use]
9 extern crate macro_rules;
10
11 // Don't lint on derives that derive `Default`
12 // See https://github.com/rust-lang/rust-clippy/issues/6545
13 #[derive(FieldReassignWithDefault)]
14 struct DerivedStruct;
15
16 #[derive(Default)]
17 struct A {
18     i: i32,
19     j: i64,
20 }
21
22 struct B {
23     i: i32,
24     j: i64,
25 }
26
27 #[derive(Default)]
28 struct C {
29     i: Vec<i32>,
30     j: i64,
31 }
32 /// Implements .next() that returns a different number each time.
33 struct SideEffect(i32);
34
35 impl SideEffect {
36     fn new() -> SideEffect {
37         SideEffect(0)
38     }
39     fn next(&mut self) -> i32 {
40         self.0 += 1;
41         self.0
42     }
43 }
44
45 fn main() {
46     // wrong, produces first error in stderr
47     let mut a: A = Default::default();
48     a.i = 42;
49
50     // right
51     let mut a: A = Default::default();
52
53     // right
54     let a = A {
55         i: 42,
56         ..Default::default()
57     };
58
59     // right
60     let mut a: A = Default::default();
61     if a.i == 0 {
62         a.j = 12;
63     }
64
65     // right
66     let mut a: A = Default::default();
67     let b = 5;
68
69     // right
70     let mut b = 32;
71     let mut a: A = Default::default();
72     b = 2;
73
74     // right
75     let b: B = B { i: 42, j: 24 };
76
77     // right
78     let mut b: B = B { i: 42, j: 24 };
79     b.i = 52;
80
81     // right
82     let mut b = B { i: 15, j: 16 };
83     let mut a: A = Default::default();
84     b.i = 2;
85
86     // wrong, produces second error in stderr
87     let mut a: A = Default::default();
88     a.j = 43;
89     a.i = 42;
90
91     // wrong, produces third error in stderr
92     let mut a: A = Default::default();
93     a.i = 42;
94     a.j = 43;
95     a.j = 44;
96
97     // wrong, produces fourth error in stderr
98     let mut a = A::default();
99     a.i = 42;
100
101     // wrong, but does not produce an error in stderr, because we can't produce a correct kind of
102     // suggestion with current implementation
103     let mut c: (i32, i32) = Default::default();
104     c.0 = 42;
105     c.1 = 21;
106
107     // wrong, produces the fifth error in stderr
108     let mut a: A = Default::default();
109     a.i = Default::default();
110
111     // wrong, produces the sixth error in stderr
112     let mut a: A = Default::default();
113     a.i = Default::default();
114     a.j = 45;
115
116     // right, because an assignment refers to another field
117     let mut x = A::default();
118     x.i = 42;
119     x.j = 21 + x.i as i64;
120
121     // right, we bail out if there's a reassignment to the same variable, since there is a risk of
122     // side-effects affecting the outcome
123     let mut x = A::default();
124     let mut side_effect = SideEffect::new();
125     x.i = side_effect.next();
126     x.j = 2;
127     x.i = side_effect.next();
128
129     // don't lint - some private fields
130     let mut x = m::F::default();
131     x.a = 1;
132
133     // don't expand macros in the suggestion (#6522)
134     let mut a: C = C::default();
135     a.i = vec![1];
136
137     // Don't lint in external macros
138     field_reassign_with_default!();
139
140     // be sure suggestion is correct with generics
141     let mut a: Wrapper<bool> = Default::default();
142     a.i = true;
143
144     let mut a: WrapperMulti<i32, i64> = Default::default();
145     a.i = 42;
146 }
147
148 mod m {
149     #[derive(Default)]
150     pub struct F {
151         pub a: u64,
152         b: u64,
153     }
154 }
155
156 #[derive(Default)]
157 struct Wrapper<T> {
158     i: T,
159 }
160
161 #[derive(Default)]
162 struct WrapperMulti<T, U> {
163     i: T,
164     j: U,
165 }