@@ -4,6 +4,7 @@ use clippy_utils::macros::span_is_local;
44use clippy_utils:: msrvs:: { self , Msrv } ;
55use clippy_utils:: source:: { HasSession , snippet_with_applicability} ;
66use clippy_utils:: ty:: implements_trait;
7+ use clippy_utils:: visitors:: is_local_used;
78use clippy_utils:: { higher, peel_blocks_with_stmt, span_contains_comment} ;
89use rustc_ast:: ast:: LitKind ;
910use rustc_ast:: { RangeLimits , UnOp } ;
@@ -43,7 +44,7 @@ pub(super) fn check<'tcx>(
4344 && let ExprKind :: Block ( ..) = body. kind
4445 // Check if the body is an assignment to a slice element.
4546 && let ExprKind :: Assign ( assignee, assignval, _) = peel_blocks_with_stmt ( body) . kind
46- && let ExprKind :: Index ( slice, _ , _) = assignee. kind
47+ && let ExprKind :: Index ( slice, idx , _) = assignee. kind
4748 // Check if `len()` is used for the range end.
4849 && let ExprKind :: MethodCall ( path, recv, ..) = end. kind
4950 && path. ident . name == sym:: len
@@ -58,6 +59,10 @@ pub(super) fn check<'tcx>(
5859 // The `fill` method requires that the slice's element type implements the `Clone` trait.
5960 && let Some ( clone_trait) = cx. tcx . lang_items ( ) . clone_trait ( )
6061 && implements_trait ( cx, cx. typeck_results ( ) . expr_ty ( slice) , clone_trait, & [ ] )
62+ // https://github.com/rust-lang/rust-clippy/issues/14192
63+ && let ExprKind :: Path ( Resolved ( _, idx_path) ) = idx. kind
64+ && let Res :: Local ( idx_hir) = idx_path. res
65+ && !is_local_used ( cx, assignval, idx_hir)
6166 {
6267 sugg ( cx, body, expr, slice. span , assignval. span ) ;
6368 }
0 commit comments