@@ -16,7 +16,7 @@ use rustc_lint::{LateContext, LateLintPass, LintContext as _};
1616use rustc_middle:: ty;
1717use rustc_session:: impl_lint_pass;
1818use rustc_span:: source_map:: Spanned ;
19- use rustc_span:: { Symbol , sym} ;
19+ use rustc_span:: { Symbol , SyntaxContext , sym} ;
2020use std:: iter;
2121
2222declare_clippy_lint ! {
@@ -92,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
9292 return ;
9393 }
9494
95- let ( strippings, bindings) = find_stripping ( cx, strip_kind, target_res, pattern, then) ;
95+ let ( strippings, bindings) = find_stripping ( cx, strip_kind, target_res, pattern, then, expr . span . ctxt ( ) ) ;
9696 if !strippings. is_empty ( ) && self . msrv . meets ( cx, msrvs:: STR_STRIP_PREFIX ) {
9797 let kind_word = match strip_kind {
9898 StripKind :: Prefix => "prefix" ,
@@ -166,8 +166,8 @@ fn len_arg<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx E
166166}
167167
168168// Returns the length of the `expr` if it's a constant string or char.
169- fn constant_length ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> Option < u128 > {
170- let value = ConstEvalCtxt :: new ( cx) . eval ( expr) ?;
169+ fn constant_length ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , ctxt : SyntaxContext ) -> Option < u128 > {
170+ let value = ConstEvalCtxt :: new ( cx) . eval_local ( expr, ctxt ) ?;
171171 match value {
172172 Constant :: Str ( value) => Some ( value. len ( ) as u128 ) ,
173173 Constant :: Char ( value) => Some ( value. len_utf8 ( ) as u128 ) ,
@@ -176,13 +176,18 @@ fn constant_length(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<u128> {
176176}
177177
178178// Tests if `expr` equals the length of the pattern.
179- fn eq_pattern_length < ' tcx > ( cx : & LateContext < ' tcx > , pattern : & Expr < ' _ > , expr : & ' tcx Expr < ' _ > ) -> bool {
179+ fn eq_pattern_length < ' tcx > (
180+ cx : & LateContext < ' tcx > ,
181+ pattern : & Expr < ' _ > ,
182+ expr : & ' tcx Expr < ' _ > ,
183+ ctxt : SyntaxContext ,
184+ ) -> bool {
180185 if let ExprKind :: Lit ( Spanned {
181186 node : LitKind :: Int ( n, _) ,
182187 ..
183188 } ) = expr. kind
184189 {
185- constant_length ( cx, pattern) . is_some_and ( |length| n == length)
190+ constant_length ( cx, pattern, ctxt ) . is_some_and ( |length| n == length)
186191 } else {
187192 len_arg ( cx, expr) . is_some_and ( |arg| eq_expr_value ( cx, pattern, arg) )
188193 }
@@ -215,6 +220,7 @@ fn find_stripping<'tcx>(
215220 target : Res ,
216221 pattern : & ' tcx Expr < ' _ > ,
217222 expr : & ' tcx Expr < ' tcx > ,
223+ ctxt : SyntaxContext ,
218224) -> ( Vec < & ' tcx Expr < ' tcx > > , FxHashMap < Symbol , usize > ) {
219225 struct StrippingFinder < ' a , ' tcx > {
220226 cx : & ' a LateContext < ' tcx > ,
@@ -223,6 +229,7 @@ fn find_stripping<'tcx>(
223229 pattern : & ' tcx Expr < ' tcx > ,
224230 results : Vec < & ' tcx Expr < ' tcx > > ,
225231 bindings : FxHashMap < Symbol , usize > ,
232+ ctxt : SyntaxContext ,
226233 }
227234
228235 impl < ' tcx > Visitor < ' tcx > for StrippingFinder < ' _ , ' tcx > {
@@ -236,7 +243,7 @@ fn find_stripping<'tcx>(
236243 {
237244 match ( self . strip_kind , start, end) {
238245 ( StripKind :: Prefix , Some ( start) , None ) => {
239- if eq_pattern_length ( self . cx , self . pattern , start) {
246+ if eq_pattern_length ( self . cx , self . pattern , start, self . ctxt ) {
240247 self . results . push ( ex) ;
241248 return ;
242249 }
@@ -252,7 +259,7 @@ fn find_stripping<'tcx>(
252259 && let Some ( left_arg) = len_arg ( self . cx , left)
253260 && let ExprKind :: Path ( left_path) = & left_arg. kind
254261 && self . cx . qpath_res ( left_path, left_arg. hir_id ) == self . target
255- && eq_pattern_length ( self . cx , self . pattern , right)
262+ && eq_pattern_length ( self . cx , self . pattern , right, self . ctxt )
256263 {
257264 self . results . push ( ex) ;
258265 return ;
@@ -280,6 +287,7 @@ fn find_stripping<'tcx>(
280287 pattern,
281288 results : vec ! [ ] ,
282289 bindings : FxHashMap :: default ( ) ,
290+ ctxt,
283291 } ;
284292 walk_expr ( & mut finder, expr) ;
285293 ( finder. results , finder. bindings )
0 commit comments