@@ -6,7 +6,7 @@ use clippy_utils::source::SpanRangeExt;
66use clippy_utils:: { def_path_def_ids, path_def_id, paths} ;
77use rustc_ast:: ast:: { LitKind , StrStyle } ;
88use rustc_hir:: def_id:: DefIdMap ;
9- use rustc_hir:: { BorrowKind , Expr , ExprKind } ;
9+ use rustc_hir:: { BorrowKind , Expr , ExprKind , HirId } ;
1010use rustc_lint:: { LateContext , LateLintPass } ;
1111use rustc_session:: impl_lint_pass;
1212use rustc_span:: { BytePos , Span } ;
@@ -104,7 +104,7 @@ enum RegexKind {
104104#[ derive( Default ) ]
105105pub struct Regex {
106106 definitions : DefIdMap < RegexKind > ,
107- loop_stack : Vec < Span > ,
107+ loop_stack : Vec < ( HirId , Span ) > ,
108108}
109109
110110impl_lint_pass ! ( Regex => [ INVALID_REGEX , TRIVIAL_REGEX , REGEX_CREATION_IN_LOOPS ] ) ;
@@ -135,7 +135,8 @@ impl<'tcx> LateLintPass<'tcx> for Regex {
135135 && let Some ( def_id) = path_def_id ( cx, fun)
136136 && let Some ( regex_kind) = self . definitions . get ( & def_id)
137137 {
138- if let Some ( & loop_span) = self . loop_stack . last ( )
138+ if let Some ( & ( loop_id, loop_span) ) = self . loop_stack . last ( )
139+ && loop_id. owner == fun. hir_id . owner
139140 && ( matches ! ( arg. kind, ExprKind :: Lit ( _) ) || const_str ( cx, arg) . is_some ( ) )
140141 {
141142 span_lint_and_help (
@@ -154,8 +155,8 @@ impl<'tcx> LateLintPass<'tcx> for Regex {
154155 RegexKind :: Bytes => check_regex ( cx, arg, false ) ,
155156 RegexKind :: BytesSet => check_set ( cx, arg, false ) ,
156157 }
157- } else if let ExprKind :: Loop ( _ , _, _, span) = expr. kind {
158- self . loop_stack . push ( span) ;
158+ } else if let ExprKind :: Loop ( block , _, _, span) = expr. kind {
159+ self . loop_stack . push ( ( block . hir_id , span) ) ;
159160 }
160161 }
161162
0 commit comments