1+ use crate :: utils:: visitors:: LocalUsedVisitor ;
12use crate :: utils:: { higher, qpath_res, snippet, span_lint_and_then} ;
23use if_chain:: if_chain;
34use rustc_errors:: Applicability ;
45use rustc_hir as hir;
56use rustc_hir:: def:: Res ;
6- use rustc_hir:: intravisit;
77use rustc_hir:: BindingAnnotation ;
88use rustc_lint:: { LateContext , LateLintPass } ;
9- use rustc_middle:: hir:: map:: Map ;
109use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1110
1211declare_clippy_lint ! {
@@ -66,10 +65,10 @@ impl<'tcx> LateLintPass<'tcx> for LetIfSeq {
6665 if let hir:: PatKind :: Binding ( mode, canonical_id, ident, None ) = local. pat. kind;
6766 if let hir:: StmtKind :: Expr ( ref if_) = expr. kind;
6867 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) ;
7069 if let hir:: ExprKind :: Block ( ref then, _) = then. kind;
7170 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) ;
7372 then {
7473 let span = stmt. span. to( if_. span) ;
7574
@@ -136,32 +135,6 @@ impl<'tcx> LateLintPass<'tcx> for LetIfSeq {
136135 }
137136}
138137
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-
165138fn check_assign < ' tcx > (
166139 cx : & LateContext < ' tcx > ,
167140 decl : hir:: HirId ,
@@ -176,18 +149,10 @@ fn check_assign<'tcx>(
176149 if let Res :: Local ( local_id) = qpath_res( cx, qpath, var. hir_id) ;
177150 if decl == local_id;
178151 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) ;
187153
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 ;
191156 }
192157
193158 return Some ( value) ;
@@ -196,9 +161,3 @@ fn check_assign<'tcx>(
196161
197162 None
198163}
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