Skip to content

Commit dbe78c3

Browse files
author
Henri Lunnikivi
committed
Fix duplicate field in suggestion
1 parent 2323ed9 commit dbe78c3

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

clippy_lints/src/field_reassign_with_default.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::utils::{match_def_path, paths, qpath_res, snippet, span_lint_and_note};
22
use if_chain::if_chain;
33
use rustc_hir::def::Res;
4+
use rustc_data_structures::fx::FxHashMap;
45
use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
56
use rustc_span::symbol::{Ident, Symbol};
67

@@ -56,7 +57,7 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
5657
// find all "later statement"'s where the fields of the binding set as
5758
// Default::default() get reassigned
5859
let mut first_assign = None;
59-
let mut assigned_fields = vec![];
60+
let mut assigned_fields = FxHashMap::default();
6061
for consequtive_statement in &block.stmts[stmt_idx + 1..] {
6162
// interrupt if the statement is a let binding (`Local`) that shadows the original
6263
// binding
@@ -70,7 +71,9 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
7071
if let Some((field_ident, assign_rhs)) = field_reassigned_by_stmt(consequtive_statement, &binding_name) {
7172
// extract and store the assigned value for help message
7273
let value_snippet = snippet(cx, assign_rhs.span, "..");
73-
assigned_fields.push((field_ident.name, value_snippet));
74+
if !assigned_fields.contains_key(&field_ident.name) {
75+
assigned_fields.insert(field_ident.name, value_snippet);
76+
}
7477

7578
// also set first instance of error for help message
7679
if first_assign.is_none() {

tests/ui/field_reassign_with_default.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@ fn main() {
5656
let mut a: A = Default::default();
5757
a.i = 42;
5858
a.j = 43;
59+
60+
// wrong, produces third error in stderr
61+
let mut a: A = Default::default();
62+
a.i = 42;
63+
a.j = 43;
64+
a.j = 44;
5965
}

tests/ui/field_reassign_with_default.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,17 @@ note: consider initializing the variable immutably with `A { i: 42, j: 43, ..Def
2323
LL | let mut a: A = Default::default();
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525

26-
error: aborting due to 2 previous errors
26+
error: field assignment outside of initializer for an instance created with Default::default()
27+
--> $DIR/field_reassign_with_default.rs:62:5
28+
|
29+
LL | a.i = 42;
30+
| ^^^^^^^^^
31+
|
32+
note: consider initializing the variable immutably with `A { i: 42, j: 43, ..Default::default() }`
33+
--> $DIR/field_reassign_with_default.rs:61:5
34+
|
35+
LL | let mut a: A = Default::default();
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37+
38+
error: aborting due to 3 previous errors
2739

0 commit comments

Comments
 (0)