@@ -91,43 +91,39 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
91
91
let stmt = & block. stmts [ stmt_idx] ;
92
92
93
93
if let StmtKind :: Local ( preceding_local) = & stmt. kind {
94
- if let Some ( ty) = & preceding_local. ty {
95
-
96
- // if all fields of the struct are not assigned, add `.. Default::default()` to the suggestion.
97
- let ext_with_default = !fields_of_type ( & binding_type) . iter ( ) . all ( |field| assigned_fields. contains_key ( & field. name ) ) ;
98
-
99
- let ty_snippet = snippet ( cx, ty. span , "_" ) ;
100
-
101
- let field_list = assigned_fields
102
- . into_iter ( )
103
- . map ( |( field, value) | format ! ( "{}: {}" , field, value) )
104
- . collect :: < Vec < String > > ( )
105
- . join ( ", " ) ;
106
-
107
- let sugg = if ext_with_default {
108
- format ! ( "{} {{ {}, ..Default::default() }}" , ty_snippet, field_list)
109
- } else {
110
- format ! ( "{} {{ {} }}" , ty_snippet, field_list)
111
- } ;
112
-
113
- // span lint once per statement that binds default
114
- span_lint_and_note (
115
- cx,
116
- FIELD_REASSIGN_WITH_DEFAULT ,
117
- first_assign. unwrap_or_else ( || unreachable ! ( ) ) . span ,
118
- "field assignment outside of initializer for an instance created with Default::default()" ,
119
- Some ( preceding_local. span ) ,
120
- & format ! ( "consider initializing the variable with `{}`" , sugg) ,
121
- ) ;
122
94
123
- }
95
+ // if all fields of the struct are not assigned, add `.. Default::default()` to the suggestion.
96
+ let ext_with_default = !fields_of_type ( & binding_type) . iter ( ) . all ( |field| assigned_fields. contains_key ( & field. name ) ) ;
97
+
98
+ let field_list = assigned_fields
99
+ . into_iter ( )
100
+ . map ( |( field, value) | format ! ( "{}: {}" , field, value) )
101
+ . collect :: < Vec < String > > ( )
102
+ . join ( ", " ) ;
103
+
104
+ let sugg = if ext_with_default {
105
+ format ! ( "{} {{ {}, ..Default::default() }}" , binding_type, field_list)
106
+ } else {
107
+ format ! ( "{} {{ {} }}" , binding_type, field_list)
108
+ } ;
109
+
110
+ // span lint once per statement that binds default
111
+ span_lint_and_note (
112
+ cx,
113
+ FIELD_REASSIGN_WITH_DEFAULT ,
114
+ first_assign. unwrap_or_else ( || unreachable ! ( ) ) . span ,
115
+ "field assignment outside of initializer for an instance created with Default::default()" ,
116
+ Some ( preceding_local. span ) ,
117
+ & format ! ( "consider initializing the variable with `{}`" , sugg) ,
118
+ ) ;
119
+
124
120
}
125
121
}
126
122
}
127
123
}
128
124
}
129
125
130
- /// Returns the indices, identifiers and types of bindings set as `Default::default()`.
126
+ /// Returns the block indices, identifiers and types of bindings set as `Default::default()`.
131
127
fn enumerate_bindings_using_default < ' cx , ' hir > ( cx : & LateContext < ' cx > , block : & Block < ' hir > ) -> Vec < ( usize , Symbol , & ' cx TyS < ' cx > ) > {
132
128
block
133
129
. stmts
0 commit comments