@@ -16,7 +16,7 @@ use rustc_lint::{LateContext, LateLintPass, LintContext as _};
16
16
use rustc_middle:: ty;
17
17
use rustc_session:: impl_lint_pass;
18
18
use rustc_span:: source_map:: Spanned ;
19
- use rustc_span:: { Symbol , sym} ;
19
+ use rustc_span:: { Symbol , SyntaxContext , sym} ;
20
20
use std:: iter;
21
21
22
22
declare_clippy_lint ! {
@@ -92,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
92
92
return ;
93
93
}
94
94
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 ( ) ) ;
96
96
if !strippings. is_empty ( ) && self . msrv . meets ( cx, msrvs:: STR_STRIP_PREFIX ) {
97
97
let kind_word = match strip_kind {
98
98
StripKind :: Prefix => "prefix" ,
@@ -166,8 +166,8 @@ fn len_arg<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx E
166
166
}
167
167
168
168
// 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 ) ?;
171
171
match value {
172
172
Constant :: Str ( value) => Some ( value. len ( ) as u128 ) ,
173
173
Constant :: Char ( value) => Some ( value. len_utf8 ( ) as u128 ) ,
@@ -176,13 +176,18 @@ fn constant_length(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<u128> {
176
176
}
177
177
178
178
// 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 {
180
185
if let ExprKind :: Lit ( Spanned {
181
186
node : LitKind :: Int ( n, _) ,
182
187
..
183
188
} ) = expr. kind
184
189
{
185
- constant_length ( cx, pattern) . is_some_and ( |length| n == length)
190
+ constant_length ( cx, pattern, ctxt ) . is_some_and ( |length| n == length)
186
191
} else {
187
192
len_arg ( cx, expr) . is_some_and ( |arg| eq_expr_value ( cx, pattern, arg) )
188
193
}
@@ -215,6 +220,7 @@ fn find_stripping<'tcx>(
215
220
target : Res ,
216
221
pattern : & ' tcx Expr < ' _ > ,
217
222
expr : & ' tcx Expr < ' tcx > ,
223
+ ctxt : SyntaxContext ,
218
224
) -> ( Vec < & ' tcx Expr < ' tcx > > , FxHashMap < Symbol , usize > ) {
219
225
struct StrippingFinder < ' a , ' tcx > {
220
226
cx : & ' a LateContext < ' tcx > ,
@@ -223,6 +229,7 @@ fn find_stripping<'tcx>(
223
229
pattern : & ' tcx Expr < ' tcx > ,
224
230
results : Vec < & ' tcx Expr < ' tcx > > ,
225
231
bindings : FxHashMap < Symbol , usize > ,
232
+ ctxt : SyntaxContext ,
226
233
}
227
234
228
235
impl < ' tcx > Visitor < ' tcx > for StrippingFinder < ' _ , ' tcx > {
@@ -236,7 +243,7 @@ fn find_stripping<'tcx>(
236
243
{
237
244
match ( self . strip_kind , start, end) {
238
245
( 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 ) {
240
247
self . results . push ( ex) ;
241
248
return ;
242
249
}
@@ -252,7 +259,7 @@ fn find_stripping<'tcx>(
252
259
&& let Some ( left_arg) = len_arg ( self . cx , left)
253
260
&& let ExprKind :: Path ( left_path) = & left_arg. kind
254
261
&& 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 )
256
263
{
257
264
self . results . push ( ex) ;
258
265
return ;
@@ -280,6 +287,7 @@ fn find_stripping<'tcx>(
280
287
pattern,
281
288
results : vec ! [ ] ,
282
289
bindings : FxHashMap :: default ( ) ,
290
+ ctxt,
283
291
} ;
284
292
walk_expr ( & mut finder, expr) ;
285
293
( finder. results , finder. bindings )
0 commit comments