mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-14 17:07:17 +00:00
Fix default initialized fields in suggestion
The default value for a field type does not necessarily match the default value for that field in the struct Default.
This commit is contained in:
parent
1f9e4cbe0d
commit
8bdf34e10c
2 changed files with 2 additions and 8 deletions
|
@ -165,12 +165,6 @@ impl LateLintPass<'_> for Default {
|
||||||
let stmt = &block.stmts[stmt_idx];
|
let stmt = &block.stmts[stmt_idx];
|
||||||
|
|
||||||
if let StmtKind::Local(preceding_local) = &stmt.kind {
|
if let StmtKind::Local(preceding_local) = &stmt.kind {
|
||||||
// filter out fields like `= Default::default()`, because the FRU already covers them
|
|
||||||
let assigned_fields = assigned_fields
|
|
||||||
.into_iter()
|
|
||||||
.filter(|(_, rhs)| !is_expr_default(rhs, cx))
|
|
||||||
.collect::<Vec<(Symbol, &Expr<'_>)>>();
|
|
||||||
|
|
||||||
// if all fields of the struct are not assigned, add `.. Default::default()` to the suggestion.
|
// if all fields of the struct are not assigned, add `.. Default::default()` to the suggestion.
|
||||||
let ext_with_default = !fields_of_type(binding_type)
|
let ext_with_default = !fields_of_type(binding_type)
|
||||||
.iter()
|
.iter()
|
||||||
|
|
|
@ -53,7 +53,7 @@ error: field assignment outside of initializer for an instance created with Defa
|
||||||
LL | a.i = Default::default();
|
LL | a.i = Default::default();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
note: consider initializing the variable with `A::default()` and removing relevant reassignments
|
note: consider initializing the variable with `A { i: Default::default(), ..Default::default() }` and removing relevant reassignments
|
||||||
--> $DIR/field_reassign_with_default.rs:90:5
|
--> $DIR/field_reassign_with_default.rs:90:5
|
||||||
|
|
|
|
||||||
LL | let mut a: A = Default::default();
|
LL | let mut a: A = Default::default();
|
||||||
|
@ -65,7 +65,7 @@ error: field assignment outside of initializer for an instance created with Defa
|
||||||
LL | a.i = Default::default();
|
LL | a.i = Default::default();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
note: consider initializing the variable with `A { j: 45, ..Default::default() }` and removing relevant reassignments
|
note: consider initializing the variable with `A { i: Default::default(), j: 45 }` and removing relevant reassignments
|
||||||
--> $DIR/field_reassign_with_default.rs:94:5
|
--> $DIR/field_reassign_with_default.rs:94:5
|
||||||
|
|
|
|
||||||
LL | let mut a: A = Default::default();
|
LL | let mut a: A = Default::default();
|
||||||
|
|
Loading…
Reference in a new issue