Skip to content

Commit 7820cb1

Browse files
committed
Add tests for
* `dst.len()` as the end of the range with loop counters * the increment of the loop counter at the top of the loop
1 parent 2a0e45b commit 7820cb1

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

tests/ui/manual_memcpy/with_loop_counters.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ pub fn manual_copy_with_counters(src: &[i32], dst: &mut [i32], dst2: &mut [i32])
3737
count += 1;
3838
}
3939

40+
let mut count = 2;
41+
for i in 0..dst.len() {
42+
dst[i] = src[count];
43+
count += 1;
44+
}
45+
4046
let mut count = 5;
4147
for i in 3..10 {
4248
dst[i] = src[count];
@@ -66,6 +72,17 @@ pub fn manual_copy_with_counters(src: &[i32], dst: &mut [i32], dst2: &mut [i32])
6672
dst[i] = src[count];
6773
count += 1
6874
}
75+
76+
// make sure ones where the increment is not at the end of the loop.
77+
// As a possible enhancement, one could adjust the offset in the suggestion according to
78+
// the position. For example, if the increment is at the top of the loop;
79+
// treating the loop counter as if it were initialized 1 greater than the original value.
80+
let mut count = 0;
81+
#[allow(clippy::needless_range_loop)]
82+
for i in 0..src.len() {
83+
count += 1;
84+
dst[i] = src[count];
85+
}
6986
}
7087

7188
fn main() {}

tests/ui/manual_memcpy/with_loop_counters.stderr

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,23 @@ LL | | }
5757
error: it looks like you're manually copying between slices
5858
--> $DIR/with_loop_counters.rs:41:5
5959
|
60+
LL | / for i in 0..dst.len() {
61+
LL | | dst[i] = src[count];
62+
LL | | count += 1;
63+
LL | | }
64+
| |_____^ help: try replacing the loop by: `dst.clone_from_slice(&src[2..(dst.len() + 2)]);`
65+
66+
error: it looks like you're manually copying between slices
67+
--> $DIR/with_loop_counters.rs:47:5
68+
|
6069
LL | / for i in 3..10 {
6170
LL | | dst[i] = src[count];
6271
LL | | count += 1;
6372
LL | | }
6473
| |_____^ help: try replacing the loop by: `dst[3..10].clone_from_slice(&src[5..(10 + 5 - 3)]);`
6574

6675
error: it looks like you're manually copying between slices
67-
--> $DIR/with_loop_counters.rs:48:5
76+
--> $DIR/with_loop_counters.rs:54:5
6877
|
6978
LL | / for i in 0..src.len() {
7079
LL | | dst[count] = src[i];
@@ -81,7 +90,7 @@ LL | dst2[30..(src.len() + 30)].clone_from_slice(&src[..]);
8190
|
8291

8392
error: it looks like you're manually copying between slices
84-
--> $DIR/with_loop_counters.rs:58:5
93+
--> $DIR/with_loop_counters.rs:64:5
8594
|
8695
LL | / for i in 0..1 << 1 {
8796
LL | | dst[count] = src[i + 2];
@@ -90,13 +99,13 @@ LL | | }
9099
| |_____^ help: try replacing the loop by: `dst[(0 << 1)..((1 << 1) + (0 << 1))].clone_from_slice(&src[2..((1 << 1) + 2)]);`
91100

92101
error: it looks like you're manually copying between slices
93-
--> $DIR/with_loop_counters.rs:65:5
102+
--> $DIR/with_loop_counters.rs:71:5
94103
|
95104
LL | / for i in 3..src.len() {
96105
LL | | dst[i] = src[count];
97106
LL | | count += 1
98107
LL | | }
99108
| |_____^ help: try replacing the loop by: `dst[3..src.len()].clone_from_slice(&src[..(src.len() - 3)]);`
100109

101-
error: aborting due to 10 previous errors
110+
error: aborting due to 11 previous errors
102111

0 commit comments

Comments
 (0)