1
+ use crate :: utils:: visitors:: LocalUsedVisitor ;
1
2
use crate :: utils:: { higher, qpath_res, snippet, span_lint_and_then} ;
2
3
use if_chain:: if_chain;
3
4
use rustc_errors:: Applicability ;
4
5
use rustc_hir as hir;
5
6
use rustc_hir:: def:: Res ;
6
- use rustc_hir:: intravisit;
7
7
use rustc_hir:: BindingAnnotation ;
8
8
use rustc_lint:: { LateContext , LateLintPass } ;
9
- use rustc_middle:: hir:: map:: Map ;
10
9
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
11
10
12
11
declare_clippy_lint ! {
@@ -66,10 +65,10 @@ impl<'tcx> LateLintPass<'tcx> for LetIfSeq {
66
65
if let hir:: PatKind :: Binding ( mode, canonical_id, ident, None ) = local. pat. kind;
67
66
if let hir:: StmtKind :: Expr ( ref if_) = expr. kind;
68
67
if let Some ( ( ref cond, ref then, ref else_) ) = higher:: if_block( & if_) ;
69
- if !used_in_expr ( cx , canonical_id, cond) ;
68
+ if !LocalUsedVisitor :: new ( canonical_id) . check_expr ( cond) ;
70
69
if let hir:: ExprKind :: Block ( ref then, _) = then. kind;
71
70
if let Some ( value) = check_assign( cx, canonical_id, & * then) ;
72
- if !used_in_expr ( cx , canonical_id, value) ;
71
+ if !LocalUsedVisitor :: new ( canonical_id) . check_expr ( value) ;
73
72
then {
74
73
let span = stmt. span. to( if_. span) ;
75
74
@@ -136,32 +135,6 @@ impl<'tcx> LateLintPass<'tcx> for LetIfSeq {
136
135
}
137
136
}
138
137
139
- struct UsedVisitor < ' a , ' tcx > {
140
- cx : & ' a LateContext < ' tcx > ,
141
- id : hir:: HirId ,
142
- used : bool ,
143
- }
144
-
145
- impl < ' a , ' tcx > intravisit:: Visitor < ' tcx > for UsedVisitor < ' a , ' tcx > {
146
- type Map = Map < ' tcx > ;
147
-
148
- fn visit_expr ( & mut self , expr : & ' tcx hir:: Expr < ' _ > ) {
149
- if_chain ! {
150
- if let hir:: ExprKind :: Path ( ref qpath) = expr. kind;
151
- if let Res :: Local ( local_id) = qpath_res( self . cx, qpath, expr. hir_id) ;
152
- if self . id == local_id;
153
- then {
154
- self . used = true ;
155
- return ;
156
- }
157
- }
158
- intravisit:: walk_expr ( self , expr) ;
159
- }
160
- fn nested_visit_map ( & mut self ) -> intravisit:: NestedVisitorMap < Self :: Map > {
161
- intravisit:: NestedVisitorMap :: None
162
- }
163
- }
164
-
165
138
fn check_assign < ' tcx > (
166
139
cx : & LateContext < ' tcx > ,
167
140
decl : hir:: HirId ,
@@ -176,18 +149,10 @@ fn check_assign<'tcx>(
176
149
if let Res :: Local ( local_id) = qpath_res( cx, qpath, var. hir_id) ;
177
150
if decl == local_id;
178
151
then {
179
- let mut v = UsedVisitor {
180
- cx,
181
- id: decl,
182
- used: false ,
183
- } ;
184
-
185
- for s in block. stmts. iter( ) . take( block. stmts. len( ) -1 ) {
186
- intravisit:: walk_stmt( & mut v, s) ;
152
+ let mut v = LocalUsedVisitor :: new( decl) ;
187
153
188
- if v. used {
189
- return None ;
190
- }
154
+ if block. stmts. iter( ) . take( block. stmts. len( ) -1 ) . any( |stmt| v. check_stmt( stmt) ) {
155
+ return None ;
191
156
}
192
157
193
158
return Some ( value) ;
@@ -196,9 +161,3 @@ fn check_assign<'tcx>(
196
161
197
162
None
198
163
}
199
-
200
- fn used_in_expr < ' tcx > ( cx : & LateContext < ' tcx > , id : hir:: HirId , expr : & ' tcx hir:: Expr < ' _ > ) -> bool {
201
- let mut v = UsedVisitor { cx, id, used : false } ;
202
- intravisit:: walk_expr ( & mut v, expr) ;
203
- v. used
204
- }
0 commit comments