@@ -39,15 +39,21 @@ pub(super) fn check<'tcx>(
3939 var : canonical_id,
4040 indexed_mut : FxHashSet :: default ( ) ,
4141 indexed_indirectly : FxHashMap :: default ( ) ,
42+ unnamed_indexed_indirectly : false ,
4243 indexed_directly : FxIndexMap :: default ( ) ,
44+ unnamed_indexed_directly : false ,
4345 referenced : FxHashSet :: default ( ) ,
4446 nonindex : false ,
4547 prefer_mutable : false ,
4648 } ;
4749 walk_expr ( & mut visitor, body) ;
4850
4951 // 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+ {
5157 let ( indexed, ( indexed_extent, indexed_ty) ) = visitor
5258 . indexed_directly
5359 . into_iter ( )
@@ -226,9 +232,13 @@ struct VarVisitor<'a, 'tcx> {
226232 indexed_mut : FxHashSet < Symbol > ,
227233 /// indirectly indexed variables (`v[(i + 4) % N]`), the extend is `None` for global
228234 indexed_indirectly : FxHashMap < Symbol , Option < region:: Scope > > ,
235+ /// indirectly indexed literals, like `[1, 2, 3][(i + 4) % N]`
236+ unnamed_indexed_indirectly : bool ,
229237 /// subset of `indexed` of vars that are indexed directly: `v[i]`
230238 /// this will not contain cases like `v[calc_index(i)]` or `v[(i + 4) % N]`
231239 indexed_directly : FxIndexMap < Symbol , ( Option < region:: Scope > , Ty < ' tcx > ) > ,
240+ /// directly indexed literals, like `[1, 2, 3][i]`
241+ unnamed_indexed_directly : bool ,
232242 /// Any names that are used outside an index operation.
233243 /// Used to detect things like `&mut vec` used together with `vec[i]`
234244 referenced : FxHashSet < Symbol > ,
@@ -242,6 +252,7 @@ struct VarVisitor<'a, 'tcx> {
242252
243253impl < ' tcx > VarVisitor < ' _ , ' tcx > {
244254 fn check ( & mut self , idx : & ' tcx Expr < ' _ > , seqexpr : & ' tcx Expr < ' _ > , expr : & ' tcx Expr < ' _ > ) -> bool {
255+ let index_used_directly = matches ! ( idx. kind, ExprKind :: Path ( _) ) ;
245256 if let ExprKind :: Path ( ref seqpath) = seqexpr. kind
246257 // the indexed container is referenced by a name
247258 && let QPath :: Resolved ( None , seqvar) = * seqpath
@@ -251,7 +262,6 @@ impl<'tcx> VarVisitor<'_, 'tcx> {
251262 if self . prefer_mutable {
252263 self . indexed_mut . insert ( seqvar. segments [ 0 ] . ident . name ) ;
253264 }
254- let index_used_directly = matches ! ( idx. kind, ExprKind :: Path ( _) ) ;
255265 let res = self . cx . qpath_res ( seqpath, seqexpr. hir_id ) ;
256266 match res {
257267 Res :: Local ( hir_id) => {
@@ -286,6 +296,13 @@ impl<'tcx> VarVisitor<'_, 'tcx> {
286296 } ,
287297 _ => ( ) ,
288298 }
299+ } else if let ExprKind :: Repeat ( ..) | ExprKind :: Array ( ..) = seqexpr. kind {
300+ if index_used_directly {
301+ self . unnamed_indexed_directly = true ;
302+ } else {
303+ self . unnamed_indexed_indirectly = true ;
304+ }
305+ return false ;
289306 }
290307 true
291308 }
0 commit comments