1
1
use crate :: utils:: { match_def_path, paths, qpath_res, snippet, span_lint_and_note} ;
2
2
use if_chain:: if_chain;
3
- use rustc_hir:: def:: Res ;
4
3
use rustc_data_structures:: fx:: FxHashMap ;
4
+ use rustc_hir:: def:: Res ;
5
5
use rustc_hir:: { Block , Expr , ExprKind , PatKind , QPath , Stmt , StmtKind } ;
6
- use rustc_span:: symbol:: { Ident , Symbol } ;
7
6
use rustc_middle:: ty:: { self , Adt , TyS } ;
7
+ use rustc_span:: symbol:: { Ident , Symbol } ;
8
8
9
9
use rustc_lint:: { LateContext , LateLintPass } ;
10
10
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -41,11 +41,11 @@ declare_lint_pass!(FieldReassignWithDefault => [FIELD_REASSIGN_WITH_DEFAULT]);
41
41
42
42
impl LateLintPass < ' _ > for FieldReassignWithDefault {
43
43
fn check_block ( & mut self , cx : & LateContext < ' _ > , block : & Block < ' _ > ) {
44
-
45
44
// find all binding statements like `let mut _ = T::default()` where `T::default()` is the
46
45
// `default` method of the `Default` trait. and store statement index in current block being
47
46
// checked and the name of the bound variable
48
- let binding_statements_using_default: Vec < ( usize , Symbol , & TyS < ' _ > ) > = enumerate_bindings_using_default ( cx, block) ;
47
+ let binding_statements_using_default: Vec < ( usize , Symbol , & TyS < ' _ > ) > =
48
+ enumerate_bindings_using_default ( cx, block) ;
49
49
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
@@ -69,7 +69,9 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
69
69
// shadow a binding
70
70
else {
71
71
// find out if and which field was set by this `consequtive_statement`
72
- if let Some ( ( field_ident, assign_rhs) ) = field_reassigned_by_stmt ( consequtive_statement, & binding_name) {
72
+ if let Some ( ( field_ident, assign_rhs) ) =
73
+ field_reassigned_by_stmt ( consequtive_statement, & binding_name)
74
+ {
73
75
// extract and store the assigned value for help message
74
76
let value_snippet = snippet ( cx, assign_rhs. span , ".." ) ;
75
77
@@ -91,9 +93,10 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
91
93
let stmt = & block. stmts [ stmt_idx] ;
92
94
93
95
if let StmtKind :: Local ( preceding_local) = & stmt. kind {
94
-
95
96
// 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
+ let ext_with_default = !fields_of_type ( & binding_type)
98
+ . iter ( )
99
+ . all ( |field| assigned_fields. contains_key ( & field. name ) ) ;
97
100
98
101
let field_list = assigned_fields
99
102
. into_iter ( )
@@ -116,15 +119,18 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
116
119
Some ( preceding_local. span ) ,
117
120
& format ! ( "consider initializing the variable with `{}`" , sugg) ,
118
121
) ;
119
-
120
122
}
121
123
}
122
124
}
123
125
}
124
126
}
125
127
126
- /// Returns the block indices, identifiers and types of bindings set as `Default::default()`, except for when the pattern type is a tuple.
127
- fn enumerate_bindings_using_default < ' cx , ' hir > ( cx : & LateContext < ' cx > , block : & Block < ' hir > ) -> Vec < ( usize , Symbol , & ' cx TyS < ' cx > ) > {
128
+ /// Returns the block indices, identifiers and types of bindings set as `Default::default()`, except
129
+ /// for when the pattern type is a tuple.
130
+ fn enumerate_bindings_using_default < ' cx , ' hir > (
131
+ cx : & LateContext < ' cx > ,
132
+ block : & Block < ' hir > ,
133
+ ) -> Vec < ( usize , Symbol , & ' cx TyS < ' cx > ) > {
128
134
block
129
135
. stmts
130
136
. iter ( )
@@ -155,7 +161,8 @@ fn enumerate_bindings_using_default<'cx, 'hir>(cx: &LateContext<'cx>, block: &Bl
155
161
None
156
162
}
157
163
}
158
- } ) . collect ( )
164
+ } )
165
+ . collect ( )
159
166
}
160
167
161
168
fn stmt_shadows_binding ( this : & Stmt < ' _ > , shadowed : & Symbol ) -> bool {
@@ -198,4 +205,4 @@ fn fields_of_type<'a>(ty: &'a TyS<'_>) -> Vec<Ident> {
198
205
}
199
206
}
200
207
vec ! [ ]
201
- }
208
+ }
0 commit comments