@@ -3,7 +3,7 @@ use crate::visitors::{expr_visitor, expr_visitor_no_bodies};
33use rustc_hir as hir;
44use rustc_hir:: intravisit:: { self , Visitor } ;
55use rustc_hir:: HirIdSet ;
6- use rustc_hir:: { Expr , ExprKind , HirId } ;
6+ use rustc_hir:: { Expr , ExprKind , HirId , Node } ;
77use rustc_infer:: infer:: TyCtxtInferExt ;
88use rustc_lint:: LateContext ;
99use rustc_middle:: hir:: nested_filter;
@@ -169,6 +169,32 @@ pub fn contains_return_break_continue_macro(expression: &Expr<'_>) -> bool {
169169
170170pub fn local_used_after_expr ( cx : & LateContext < ' _ > , local_id : HirId , after : & Expr < ' _ > ) -> bool {
171171 let Some ( block) = utils:: get_enclosing_block ( cx, local_id) else { return false } ;
172+
173+ // for _ in 1..3 {
174+ // local
175+ // }
176+ //
177+ // let closure = || local;
178+ // closure();
179+ // closure();
180+ let in_loop_or_closure = cx
181+ . tcx
182+ . hir ( )
183+ . parent_iter ( after. hir_id )
184+ . take_while ( |& ( id, _) | id != block. hir_id )
185+ . any ( |( _, node) | {
186+ matches ! (
187+ node,
188+ Node :: Expr ( Expr {
189+ kind: ExprKind :: Loop ( ..) | ExprKind :: Closure ( ..) ,
190+ ..
191+ } )
192+ )
193+ } ) ;
194+ if in_loop_or_closure {
195+ return true ;
196+ }
197+
172198 let mut used_after_expr = false ;
173199 let mut past_expr = false ;
174200 expr_visitor ( cx, |expr| {
@@ -178,7 +204,10 @@ pub fn local_used_after_expr(cx: &LateContext<'_>, local_id: HirId, after: &Expr
178204
179205 if expr. hir_id == after. hir_id {
180206 past_expr = true ;
181- } else if past_expr && utils:: path_to_local_id ( expr, local_id) {
207+ return false ;
208+ }
209+
210+ if past_expr && utils:: path_to_local_id ( expr, local_id) {
182211 used_after_expr = true ;
183212 }
184213 !used_after_expr
0 commit comments