about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJason Newcomb <jsnewcomb@pm.me>2025-02-12 15:14:36 +0000
committerGitHub <noreply@github.com>2025-02-12 15:14:36 +0000
commita342340e6deb329b8eb9f2cf57275fd282ad14c8 (patch)
treea6c29030be9e2a83af9c1511bfbf647ff087e2e1
parent32aef114c6c1fd0a3c8c8510e4d53125e69d5c5c (diff)
parent7139436f98045b515db7f15a65cafe5afc5b7527 (diff)
downloadrust-a342340e6deb329b8eb9f2cf57275fd282ad14c8.tar.gz
rust-a342340e6deb329b8eb9f2cf57275fd282ad14c8.zip
add index checks for the slice in `manual_slice_fill` (#14193)
fix #14192

changelog: [`manual_slice_fill`]: resolve FP caused by missing index
checks for the slice
-rw-r--r--clippy_lints/src/loops/manual_slice_fill.rs7
-rw-r--r--tests/ui/manual_slice_fill.fixed18
-rw-r--r--tests/ui/manual_slice_fill.rs18
3 files changed, 40 insertions, 3 deletions
diff --git a/clippy_lints/src/loops/manual_slice_fill.rs b/clippy_lints/src/loops/manual_slice_fill.rs
index 7c656423579..bece83eaf3d 100644
--- a/clippy_lints/src/loops/manual_slice_fill.rs
+++ b/clippy_lints/src/loops/manual_slice_fill.rs
@@ -4,6 +4,7 @@ use clippy_utils::macros::span_is_local;
 use clippy_utils::msrvs::{self, Msrv};
 use clippy_utils::source::{HasSession, snippet_with_applicability};
 use clippy_utils::ty::implements_trait;
+use clippy_utils::visitors::is_local_used;
 use clippy_utils::{higher, peel_blocks_with_stmt, span_contains_comment};
 use rustc_ast::ast::LitKind;
 use rustc_ast::{RangeLimits, UnOp};
@@ -43,7 +44,7 @@ pub(super) fn check<'tcx>(
         && let ExprKind::Block(..) = body.kind
         // Check if the body is an assignment to a slice element.
         && let ExprKind::Assign(assignee, assignval, _) = peel_blocks_with_stmt(body).kind
-        && let ExprKind::Index(slice, _, _) = assignee.kind
+        && let ExprKind::Index(slice, idx, _) = assignee.kind
         // Check if `len()` is used for the range end.
         && let ExprKind::MethodCall(path, recv,..) = end.kind
         && path.ident.name == sym::len
@@ -58,6 +59,10 @@ pub(super) fn check<'tcx>(
         // The `fill` method requires that the slice's element type implements the `Clone` trait.
         && let Some(clone_trait) = cx.tcx.lang_items().clone_trait()
         && implements_trait(cx, cx.typeck_results().expr_ty(slice), clone_trait, &[])
+        // https://github.com/rust-lang/rust-clippy/issues/14192
+        && let ExprKind::Path(Resolved(_, idx_path)) = idx.kind
+        && let Res::Local(idx_hir) = idx_path.res
+        && !is_local_used(cx, assignval, idx_hir)
     {
         sugg(cx, body, expr, slice.span, assignval.span);
     }
diff --git a/tests/ui/manual_slice_fill.fixed b/tests/ui/manual_slice_fill.fixed
index 397a156a2dc..80e271117fc 100644
--- a/tests/ui/manual_slice_fill.fixed
+++ b/tests/ui/manual_slice_fill.fixed
@@ -1,5 +1,5 @@
 #![warn(clippy::manual_slice_fill)]
-#![allow(clippy::needless_range_loop)]
+#![allow(clippy::needless_range_loop, clippy::useless_vec)]
 
 macro_rules! assign_element {
     ($slice:ident, $index:expr) => {
@@ -99,3 +99,19 @@ fn should_not_lint() {
         *i = None;
     }
 }
+
+fn issue_14192() {
+    let mut tmp = vec![0; 3];
+
+    for i in 0..tmp.len() {
+        tmp[i] = i;
+    }
+
+    for i in 0..tmp.len() {
+        tmp[i] = 2 + i;
+    }
+
+    for i in 0..tmp.len() {
+        tmp[0] = i;
+    }
+}
diff --git a/tests/ui/manual_slice_fill.rs b/tests/ui/manual_slice_fill.rs
index c25127ca613..75a391f4243 100644
--- a/tests/ui/manual_slice_fill.rs
+++ b/tests/ui/manual_slice_fill.rs
@@ -1,5 +1,5 @@
 #![warn(clippy::manual_slice_fill)]
-#![allow(clippy::needless_range_loop)]
+#![allow(clippy::needless_range_loop, clippy::useless_vec)]
 
 macro_rules! assign_element {
     ($slice:ident, $index:expr) => {
@@ -108,3 +108,19 @@ fn should_not_lint() {
         *i = None;
     }
 }
+
+fn issue_14192() {
+    let mut tmp = vec![0; 3];
+
+    for i in 0..tmp.len() {
+        tmp[i] = i;
+    }
+
+    for i in 0..tmp.len() {
+        tmp[i] = 2 + i;
+    }
+
+    for i in 0..tmp.len() {
+        tmp[0] = i;
+    }
+}