@@ -39,15 +39,21 @@ pub(super) fn check<'tcx>(
39
39
var : canonical_id,
40
40
indexed_mut : FxHashSet :: default ( ) ,
41
41
indexed_indirectly : FxHashMap :: default ( ) ,
42
+ unnamed_indexed_indirectly : false ,
42
43
indexed_directly : FxIndexMap :: default ( ) ,
44
+ unnamed_indexed_directly : false ,
43
45
referenced : FxHashSet :: default ( ) ,
44
46
nonindex : false ,
45
47
prefer_mutable : false ,
46
48
} ;
47
49
walk_expr ( & mut visitor, body) ;
48
50
49
51
// linting condition: we only indexed one variable, and indexed it directly
50
- if visitor. indexed_indirectly . is_empty ( ) && visitor. indexed_directly . len ( ) == 1 {
52
+ if visitor. indexed_indirectly . is_empty ( )
53
+ && !visitor. unnamed_indexed_indirectly
54
+ && !visitor. unnamed_indexed_directly
55
+ && visitor. indexed_directly . len ( ) == 1
56
+ {
51
57
let ( indexed, ( indexed_extent, indexed_ty) ) = visitor
52
58
. indexed_directly
53
59
. into_iter ( )
@@ -217,6 +223,7 @@ fn is_end_eq_array_len<'tcx>(
217
223
false
218
224
}
219
225
226
+ #[ expect( clippy:: struct_excessive_bools) ]
220
227
struct VarVisitor < ' a , ' tcx > {
221
228
/// context reference
222
229
cx : & ' a LateContext < ' tcx > ,
@@ -226,9 +233,13 @@ struct VarVisitor<'a, 'tcx> {
226
233
indexed_mut : FxHashSet < Symbol > ,
227
234
/// indirectly indexed variables (`v[(i + 4) % N]`), the extend is `None` for global
228
235
indexed_indirectly : FxHashMap < Symbol , Option < region:: Scope > > ,
236
+ /// indirectly indexed literals, like `[1, 2, 3][(i + 4) % N]`
237
+ unnamed_indexed_indirectly : bool ,
229
238
/// subset of `indexed` of vars that are indexed directly: `v[i]`
230
239
/// this will not contain cases like `v[calc_index(i)]` or `v[(i + 4) % N]`
231
240
indexed_directly : FxIndexMap < Symbol , ( Option < region:: Scope > , Ty < ' tcx > ) > ,
241
+ /// directly indexed literals, like `[1, 2, 3][i]`
242
+ unnamed_indexed_directly : bool ,
232
243
/// Any names that are used outside an index operation.
233
244
/// Used to detect things like `&mut vec` used together with `vec[i]`
234
245
referenced : FxHashSet < Symbol > ,
@@ -242,6 +253,7 @@ struct VarVisitor<'a, 'tcx> {
242
253
243
254
impl < ' tcx > VarVisitor < ' _ , ' tcx > {
244
255
fn check ( & mut self , idx : & ' tcx Expr < ' _ > , seqexpr : & ' tcx Expr < ' _ > , expr : & ' tcx Expr < ' _ > ) -> bool {
256
+ let index_used_directly = matches ! ( idx. kind, ExprKind :: Path ( _) ) ;
245
257
if let ExprKind :: Path ( ref seqpath) = seqexpr. kind
246
258
// the indexed container is referenced by a name
247
259
&& let QPath :: Resolved ( None , seqvar) = * seqpath
@@ -251,7 +263,6 @@ impl<'tcx> VarVisitor<'_, 'tcx> {
251
263
if self . prefer_mutable {
252
264
self . indexed_mut . insert ( seqvar. segments [ 0 ] . ident . name ) ;
253
265
}
254
- let index_used_directly = matches ! ( idx. kind, ExprKind :: Path ( _) ) ;
255
266
let res = self . cx . qpath_res ( seqpath, seqexpr. hir_id ) ;
256
267
match res {
257
268
Res :: Local ( hir_id) => {
@@ -286,6 +297,13 @@ impl<'tcx> VarVisitor<'_, 'tcx> {
286
297
} ,
287
298
_ => ( ) ,
288
299
}
300
+ } else if let ExprKind :: Repeat ( ..) | ExprKind :: Array ( ..) = seqexpr. kind {
301
+ if index_used_directly {
302
+ self . unnamed_indexed_directly = true ;
303
+ } else {
304
+ self . unnamed_indexed_indirectly = true ;
305
+ }
306
+ return false ;
289
307
}
290
308
true
291
309
}
0 commit comments