Fix suggestion with generics for field_reassign_with_default lint

This commit is contained in:
ThibsG 2021-03-21 09:50:35 +01:00
parent 0bdaa77d95
commit 3ddaabcbc9
3 changed files with 62 additions and 1 deletions

View file

@ -136,6 +136,13 @@ fn main() {
// 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;
}
mod m {
@ -145,3 +152,14 @@ mod m {
b: u64,
}
}
#[derive(Default)]
struct Wrapper<T> {
i: T,
}
#[derive(Default)]
struct WrapperMulti<T, U> {
i: T,
j: U,
}

View file

@ -83,5 +83,29 @@ note: consider initializing the variable with `C { i: vec![1], ..Default::defaul
LL | let mut a: C = C::default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 7 previous errors
error: field assignment outside of initializer for an instance created with Default::default()
--> $DIR/field_reassign_with_default.rs:142:5
|
LL | a.i = true;
| ^^^^^^^^^^^
|
note: consider initializing the variable with `Wrapper::<bool> { i: true }` and removing relevant reassignments
--> $DIR/field_reassign_with_default.rs:141:5
|
LL | let mut a: Wrapper<bool> = Default::default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: field assignment outside of initializer for an instance created with Default::default()
--> $DIR/field_reassign_with_default.rs:145:5
|
LL | a.i = 42;
| ^^^^^^^^^
|
note: consider initializing the variable with `WrapperMulti::<i32, i64> { i: 42, ..Default::default() }` and removing relevant reassignments
--> $DIR/field_reassign_with_default.rs:144:5
|
LL | let mut a: WrapperMulti<i32, i64> = Default::default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 9 previous errors