Skip to content

Commit 5939c4d

Browse files
committed
add index checks for the slice in manual_slice_fill
1 parent 8c01600 commit 5939c4d

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

clippy_lints/src/loops/manual_slice_fill.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use clippy_utils::macros::span_is_local;
44
use clippy_utils::msrvs::{self, Msrv};
55
use clippy_utils::source::{HasSession, snippet_with_applicability};
66
use clippy_utils::ty::implements_trait;
7+
use clippy_utils::visitors::is_local_used;
78
use clippy_utils::{higher, peel_blocks_with_stmt, span_contains_comment};
89
use rustc_ast::ast::LitKind;
910
use rustc_ast::{RangeLimits, UnOp};
@@ -43,7 +44,10 @@ 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
48+
&& let ExprKind::Path(Resolved(_,idx_path)) = idx.kind
49+
&& let Res::Local(idx_hir) = idx_path.res
50+
&& !is_local_used(cx, assignval, idx_hir)
4751
// Check if `len()` is used for the range end.
4852
&& let ExprKind::MethodCall(path, recv,..) = end.kind
4953
&& path.ident.name == sym::len

tests/ui/manual_slice_fill.fixed

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,19 @@ fn should_not_lint() {
9999
*i = None;
100100
}
101101
}
102+
103+
fn issue_14192() {
104+
let mut tmp = [0; 3];
105+
106+
for i in 0..tmp.len() {
107+
tmp[i] = i;
108+
}
109+
110+
for i in 0..tmp.len() {
111+
tmp[i] = 2 + i;
112+
}
113+
114+
for i in 0..tmp.len() {
115+
tmp[0] = i;
116+
}
117+
}

tests/ui/manual_slice_fill.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,19 @@ fn should_not_lint() {
108108
*i = None;
109109
}
110110
}
111+
112+
fn issue_14192() {
113+
let mut tmp = vec![0; 3];
114+
115+
for i in 0..tmp.len() {
116+
tmp[i] = i;
117+
}
118+
119+
for i in 0..tmp.len() {
120+
tmp[i] = 2 + i;
121+
}
122+
123+
for i in 0..tmp.len() {
124+
tmp[0] = i;
125+
}
126+
}

tests/ui/manual_slice_fill.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,14 @@ LL | | // foo
3434
LL | | }
3535
| |_____^ help: try: `some_slice.fill(0);`
3636

37-
error: aborting due to 4 previous errors
37+
error: useless use of `vec!`
38+
--> tests/ui/manual_slice_fill.rs:113:19
39+
|
40+
LL | let mut tmp = vec![0; 3];
41+
| ^^^^^^^^^^ help: you can use an array directly: `[0; 3]`
42+
|
43+
= note: `-D clippy::useless-vec` implied by `-D warnings`
44+
= help: to override `-D warnings` add `#[allow(clippy::useless_vec)]`
45+
46+
error: aborting due to 5 previous errors
3847

0 commit comments

Comments
 (0)