1 // aux-build:proc_macro_derive.rs
2 // aux-build:macro_rules.rs
4 #![warn(clippy::field_reassign_with_default)]
7 extern crate proc_macro_derive;
9 extern crate macro_rules;
11 // Don't lint on derives that derive `Default`
12 // See https://github.com/rust-lang/rust-clippy/issues/6545
13 #[derive(FieldReassignWithDefault)]
32 /// Implements .next() that returns a different number each time.
33 struct SideEffect(i32);
36 fn new() -> SideEffect {
39 fn next(&mut self) -> i32 {
46 // wrong, produces first error in stderr
47 let mut a: A = Default::default();
51 let mut a: A = Default::default();
60 let mut a: A = Default::default();
66 let mut a: A = Default::default();
71 let mut a: A = Default::default();
75 let b: B = B { i: 42, j: 24 };
78 let mut b: B = B { i: 42, j: 24 };
82 let mut b = B { i: 15, j: 16 };
83 let mut a: A = Default::default();
86 // wrong, produces second error in stderr
87 let mut a: A = Default::default();
91 // wrong, produces third error in stderr
92 let mut a: A = Default::default();
97 // wrong, produces fourth error in stderr
98 let mut a = A::default();
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();
107 // wrong, produces the fifth error in stderr
108 let mut a: A = Default::default();
109 a.i = Default::default();
111 // wrong, produces the sixth error in stderr
112 let mut a: A = Default::default();
113 a.i = Default::default();
116 // right, because an assignment refers to another field
117 let mut x = A::default();
119 x.j = 21 + x.i as i64;
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();
127 x.i = side_effect.next();
129 // don't lint - some private fields
130 let mut x = m::F::default();
133 // don't expand macros in the suggestion (#6522)
134 let mut a: C = C::default();
137 // Don't lint in external macros
138 field_reassign_with_default!();