Skip to content

Commit 83f90cc

Browse files
author
Henri Lunnikivi
committed
Only look at consequtive statements
1 parent 0042ba8 commit 83f90cc

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

clippy_lints/src/field_reassign_with_default.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
5050
// start from the `let mut _ = _::default();` and look at all the following
5151
// statements, see if they re-assign the fields of the binding
5252
for (stmt_idx, binding_name, binding_type) in binding_statements_using_default {
53-
// last statement of block cannot trigger the lint
53+
// the last statement of a block cannot trigger the lint
5454
if stmt_idx == block.stmts.len() - 1 {
5555
break;
5656
}
@@ -62,28 +62,28 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
6262
for consequtive_statement in &block.stmts[stmt_idx + 1..] {
6363
// interrupt if the statement is a let binding (`Local`) that shadows the original
6464
// binding
65-
if stmt_shadows_binding(consequtive_statement, &binding_name) {
65+
if stmt_shadows_binding(consequtive_statement, binding_name) {
6666
break;
6767
}
68-
// statement kinds other than `StmtKind::Local` are valid, because they cannot
69-
// shadow a binding
70-
else {
71-
// find out if and which field was set by this `consequtive_statement`
72-
if let Some((field_ident, assign_rhs)) =
73-
field_reassigned_by_stmt(consequtive_statement, &binding_name)
74-
{
75-
// extract and store the assigned value for help message
76-
let value_snippet = snippet(cx, assign_rhs.span, "..");
77-
78-
// always re-insert set value, this way the latest value is stored for output snippet
79-
assigned_fields.insert(field_ident.name, value_snippet);
80-
81-
// also set first instance of error for help message
82-
if first_assign.is_none() {
83-
first_assign = Some(consequtive_statement);
84-
}
68+
// find out if and which field was set by this `consequtive_statement`
69+
else if let Some((field_ident, assign_rhs)) =
70+
field_reassigned_by_stmt(consequtive_statement, binding_name)
71+
{
72+
// extract and store the assigned value for help message
73+
let value_snippet = snippet(cx, assign_rhs.span, "..");
74+
75+
// always re-insert set value, this way the latest value is stored for output snippet
76+
assigned_fields.insert(field_ident.name, value_snippet);
77+
78+
// also set first instance of error for help message
79+
if first_assign.is_none() {
80+
first_assign = Some(consequtive_statement);
8581
}
8682
}
83+
// interrupt also if no field was assigned, since we only want to look at consequtive statements
84+
else {
85+
break;
86+
}
8787
}
8888

8989
// if there are incorrectly assigned fields, do a span_lint_and_note to suggest
@@ -165,17 +165,17 @@ fn enumerate_bindings_using_default<'cx, 'hir>(
165165
.collect()
166166
}
167167

168-
fn stmt_shadows_binding(this: &Stmt<'_>, shadowed: &Symbol) -> bool {
168+
fn stmt_shadows_binding(this: &Stmt<'_>, shadowed: Symbol) -> bool {
169169
if let StmtKind::Local(local) = &this.kind {
170170
if let PatKind::Binding(_, _, ident, _) = local.pat.kind {
171-
return &ident.name == shadowed;
171+
return ident.name == shadowed;
172172
}
173173
}
174174
false
175175
}
176176

177177
/// Returns the reassigned field and the assigning expression (right-hand side of assign).
178-
fn field_reassigned_by_stmt<'hir>(this: &Stmt<'hir>, binding_name: &Symbol) -> Option<(Ident, &'hir Expr<'hir>)> {
178+
fn field_reassigned_by_stmt<'hir>(this: &Stmt<'hir>, binding_name: Symbol) -> Option<(Ident, &'hir Expr<'hir>)> {
179179
if_chain! {
180180
// only take assignments
181181
if let StmtKind::Semi(ref later_expr) = this.kind;
@@ -186,7 +186,7 @@ fn field_reassigned_by_stmt<'hir>(this: &Stmt<'hir>, binding_name: &Symbol) -> O
186186
if let ExprKind::Path(ref qpath) = binding.kind;
187187
if let QPath::Resolved(_, path) = qpath;
188188
if let Some(second_binding_name) = path.segments.last();
189-
if &second_binding_name.ident.name == binding_name;
189+
if second_binding_name.ident.name == binding_name;
190190
then {
191191
Some((field_ident, assign_rhs))
192192
} else {
@@ -200,7 +200,7 @@ fn fields_of_type<'a>(ty: &'a TyS<'_>) -> Vec<Ident> {
200200
if let Adt(adt, _) = ty.kind() {
201201
if adt.is_struct() {
202202
// unwrap is safe, because this is a struct and structs have only one variant
203-
let variant = &adt.variants.get(0usize.into()).unwrap();
203+
let variant = &adt.variants.get(0_usize.into()).unwrap();
204204
return variant.fields.iter().map(|f| f.ident).collect();
205205
}
206206
}

0 commit comments

Comments
 (0)