@@ -6,7 +6,7 @@ use clippy_utils::source::SpanRangeExt;
6
6
use clippy_utils:: { def_path_def_ids, path_def_id, paths} ;
7
7
use rustc_ast:: ast:: { LitKind , StrStyle } ;
8
8
use rustc_hir:: def_id:: DefIdMap ;
9
- use rustc_hir:: { BorrowKind , Expr , ExprKind } ;
9
+ use rustc_hir:: { BorrowKind , Expr , ExprKind , OwnerId } ;
10
10
use rustc_lint:: { LateContext , LateLintPass } ;
11
11
use rustc_session:: impl_lint_pass;
12
12
use rustc_span:: { BytePos , Span } ;
@@ -104,7 +104,7 @@ enum RegexKind {
104
104
#[ derive( Default ) ]
105
105
pub struct Regex {
106
106
definitions : DefIdMap < RegexKind > ,
107
- loop_stack : Vec < Span > ,
107
+ loop_stack : Vec < ( OwnerId , Span ) > ,
108
108
}
109
109
110
110
impl_lint_pass ! ( Regex => [ INVALID_REGEX , TRIVIAL_REGEX , REGEX_CREATION_IN_LOOPS ] ) ;
@@ -135,7 +135,8 @@ impl<'tcx> LateLintPass<'tcx> for Regex {
135
135
&& let Some ( def_id) = path_def_id ( cx, fun)
136
136
&& let Some ( regex_kind) = self . definitions . get ( & def_id)
137
137
{
138
- if let Some ( & loop_span) = self . loop_stack . last ( )
138
+ if let Some ( & ( loop_item_id, loop_span) ) = self . loop_stack . last ( )
139
+ && loop_item_id == fun. hir_id . owner
139
140
&& ( matches ! ( arg. kind, ExprKind :: Lit ( _) ) || const_str ( cx, arg) . is_some ( ) )
140
141
{
141
142
span_lint_and_help (
@@ -154,8 +155,8 @@ impl<'tcx> LateLintPass<'tcx> for Regex {
154
155
RegexKind :: Bytes => check_regex ( cx, arg, false ) ,
155
156
RegexKind :: BytesSet => check_set ( cx, arg, false ) ,
156
157
}
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 . owner , span) ) ;
159
160
}
160
161
}
161
162
0 commit comments