Skip to content

Commit 173e1ba

Browse files
camsteffenflip1995
authored andcommitted
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.
1 parent 4911ab1 commit 173e1ba

File tree

2 files changed

+2
-8
lines changed

2 files changed

+2
-8
lines changed

clippy_lints/src/default.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,6 @@ impl LateLintPass<'_> for Default {
165165
let stmt = &block.stmts[stmt_idx];
166166

167167
if let StmtKind::Local(preceding_local) = &stmt.kind {
168-
// filter out fields like `= Default::default()`, because the FRU already covers them
169-
let assigned_fields = assigned_fields
170-
.into_iter()
171-
.filter(|(_, rhs)| !is_expr_default(rhs, cx))
172-
.collect::<Vec<(Symbol, &Expr<'_>)>>();
173-
174168
// if all fields of the struct are not assigned, add `.. Default::default()` to the suggestion.
175169
let ext_with_default = !fields_of_type(binding_type)
176170
.iter()

tests/ui/field_reassign_with_default.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ error: field assignment outside of initializer for an instance created with Defa
5353
LL | a.i = Default::default();
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5555
|
56-
note: consider initializing the variable with `A::default()` and removing relevant reassignments
56+
note: consider initializing the variable with `A { i: Default::default(), ..Default::default() }` and removing relevant reassignments
5757
--> $DIR/field_reassign_with_default.rs:90:5
5858
|
5959
LL | let mut a: A = Default::default();
@@ -65,7 +65,7 @@ error: field assignment outside of initializer for an instance created with Defa
6565
LL | a.i = Default::default();
6666
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6767
|
68-
note: consider initializing the variable with `A { j: 45, ..Default::default() }` and removing relevant reassignments
68+
note: consider initializing the variable with `A { i: Default::default(), j: 45 }` and removing relevant reassignments
6969
--> $DIR/field_reassign_with_default.rs:94:5
7070
|
7171
LL | let mut a: A = Default::default();

0 commit comments

Comments
 (0)