about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCameron Steffen <cam.steffen94@gmail.com>2020-11-23 12:26:35 -0600
committerflip1995 <philipp.krones@embecosm.com>2021-01-02 18:25:39 +0100
commit173e1ba966d92570cbf64d65e2f7353bcab1c661 (patch)
tree15f889ec69c01d96127347adcd0441b21831d52c
parent4911ab124c481430672a3833b37075e6435ec34d (diff)
downloadrust-173e1ba966d92570cbf64d65e2f7353bcab1c661.tar.gz
rust-173e1ba966d92570cbf64d65e2f7353bcab1c661.zip
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.
-rw-r--r--clippy_lints/src/default.rs6
-rw-r--r--tests/ui/field_reassign_with_default.stderr4
2 files changed, 2 insertions, 8 deletions
diff --git a/clippy_lints/src/default.rs b/clippy_lints/src/default.rs
index f69f6f1412a..adcc17266d2 100644
--- a/clippy_lints/src/default.rs
+++ b/clippy_lints/src/default.rs
@@ -165,12 +165,6 @@ impl LateLintPass<'_> for Default {
                 let stmt = &block.stmts[stmt_idx];
 
                 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.
                     let ext_with_default = !fields_of_type(binding_type)
                         .iter()
diff --git a/tests/ui/field_reassign_with_default.stderr b/tests/ui/field_reassign_with_default.stderr
index c788ebae552..9a2bc778c3f 100644
--- a/tests/ui/field_reassign_with_default.stderr
+++ b/tests/ui/field_reassign_with_default.stderr
@@ -53,7 +53,7 @@ error: field assignment outside of initializer for an instance created with Defa
 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
    |
 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();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
-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
    |
 LL |     let mut a: A = Default::default();