@@ -4,6 +4,7 @@ use clippy_utils::macros::span_is_local;
4
4
use clippy_utils:: msrvs:: { self , Msrv } ;
5
5
use clippy_utils:: source:: { HasSession , snippet_with_applicability} ;
6
6
use clippy_utils:: ty:: implements_trait;
7
+ use clippy_utils:: visitors:: is_local_used;
7
8
use clippy_utils:: { higher, peel_blocks_with_stmt, span_contains_comment} ;
8
9
use rustc_ast:: ast:: LitKind ;
9
10
use rustc_ast:: { RangeLimits , UnOp } ;
@@ -43,7 +44,7 @@ pub(super) fn check<'tcx>(
43
44
&& let ExprKind :: Block ( ..) = body. kind
44
45
// Check if the body is an assignment to a slice element.
45
46
&& 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
47
48
// Check if `len()` is used for the range end.
48
49
&& let ExprKind :: MethodCall ( path, recv, ..) = end. kind
49
50
&& path. ident . name == sym:: len
@@ -58,6 +59,10 @@ pub(super) fn check<'tcx>(
58
59
// The `fill` method requires that the slice's element type implements the `Clone` trait.
59
60
&& let Some ( clone_trait) = cx. tcx . lang_items ( ) . clone_trait ( )
60
61
&& 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)
61
66
{
62
67
sugg ( cx, body, expr, slice. span , assignval. span ) ;
63
68
}
0 commit comments