@@ -50,7 +50,7 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
50
50
// start from the `let mut _ = _::default();` and look at all the following
51
51
// statements, see if they re-assign the fields of the binding
52
52
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
54
54
if stmt_idx == block. stmts . len ( ) - 1 {
55
55
break ;
56
56
}
@@ -62,28 +62,28 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
62
62
for consequtive_statement in & block. stmts [ stmt_idx + 1 ..] {
63
63
// interrupt if the statement is a let binding (`Local`) that shadows the original
64
64
// binding
65
- if stmt_shadows_binding ( consequtive_statement, & binding_name) {
65
+ if stmt_shadows_binding ( consequtive_statement, binding_name) {
66
66
break ;
67
67
}
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) ;
85
81
}
86
82
}
83
+ // interrupt also if no field was assigned, since we only want to look at consequtive statements
84
+ else {
85
+ break ;
86
+ }
87
87
}
88
88
89
89
// 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>(
165
165
. collect ( )
166
166
}
167
167
168
- fn stmt_shadows_binding ( this : & Stmt < ' _ > , shadowed : & Symbol ) -> bool {
168
+ fn stmt_shadows_binding ( this : & Stmt < ' _ > , shadowed : Symbol ) -> bool {
169
169
if let StmtKind :: Local ( local) = & this. kind {
170
170
if let PatKind :: Binding ( _, _, ident, _) = local. pat . kind {
171
- return & ident. name == shadowed;
171
+ return ident. name == shadowed;
172
172
}
173
173
}
174
174
false
175
175
}
176
176
177
177
/// 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 > ) > {
179
179
if_chain ! {
180
180
// only take assignments
181
181
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
186
186
if let ExprKind :: Path ( ref qpath) = binding. kind;
187
187
if let QPath :: Resolved ( _, path) = qpath;
188
188
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;
190
190
then {
191
191
Some ( ( field_ident, assign_rhs) )
192
192
} else {
@@ -200,7 +200,7 @@ fn fields_of_type<'a>(ty: &'a TyS<'_>) -> Vec<Ident> {
200
200
if let Adt ( adt, _) = ty. kind ( ) {
201
201
if adt. is_struct ( ) {
202
202
// 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 ( ) ;
204
204
return variant. fields . iter ( ) . map ( |f| f. ident ) . collect ( ) ;
205
205
}
206
206
}
0 commit comments