diff --git a/tests/ui/for_loop.rs b/tests/ui/for_loop.rs index ce8de0745..41ac824e8 100644 --- a/tests/ui/for_loop.rs +++ b/tests/ui/for_loop.rs @@ -355,114 +355,6 @@ fn partition(v: &mut [T]) -> usize { i } -const LOOP_OFFSET: usize = 5000; - -#[warn(clippy::needless_range_loop)] -pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) { - // plain manual memcpy - for i in 0..src.len() { - dst[i] = src[i]; - } - - // dst offset memcpy - for i in 0..src.len() { - dst[i + 10] = src[i]; - } - - // src offset memcpy - for i in 0..src.len() { - dst[i] = src[i + 10]; - } - - // src offset memcpy - for i in 11..src.len() { - dst[i] = src[i - 10]; - } - - // overwrite entire dst - for i in 0..dst.len() { - dst[i] = src[i]; - } - - // manual copy with branch - can't easily convert to memcpy! - for i in 0..src.len() { - dst[i] = src[i]; - if dst[i] > 5 { - break; - } - } - - // multiple copies - suggest two memcpy statements - for i in 10..256 { - dst[i] = src[i - 5]; - dst2[i + 500] = src[i] - } - - // this is a reversal - the copy lint shouldn't be triggered - for i in 10..LOOP_OFFSET { - dst[i + LOOP_OFFSET] = src[LOOP_OFFSET - i]; - } - - let some_var = 5; - // Offset in variable - for i in 10..LOOP_OFFSET { - dst[i + LOOP_OFFSET] = src[i - some_var]; - } - - // Non continuous copy - don't trigger lint - for i in 0..10 { - dst[i + i] = src[i]; - } - - let src_vec = vec![1, 2, 3, 4, 5]; - let mut dst_vec = vec![0, 0, 0, 0, 0]; - - // make sure vectors are supported - for i in 0..src_vec.len() { - dst_vec[i] = src_vec[i]; - } - - // lint should not trigger when either - // source or destination type is not - // slice-like, like DummyStruct - struct DummyStruct(i32); - - impl ::std::ops::Index for DummyStruct { - type Output = i32; - - fn index(&self, _: usize) -> &i32 { - &self.0 - } - } - - let src = DummyStruct(5); - let mut dst_vec = vec![0; 10]; - - for i in 0..10 { - dst_vec[i] = src[i]; - } - - // Simplify suggestion (issue #3004) - let src = [0, 1, 2, 3, 4]; - let mut dst = [0, 0, 0, 0, 0, 0]; - let from = 1; - - for i in from..from + src.len() { - dst[i] = src[i - from]; - } - - for i in from..from + 3 { - dst[i] = src[i - from]; - } -} - -#[warn(clippy::needless_range_loop)] -pub fn manual_clone(src: &[String], dst: &mut [String]) { - for i in 0..src.len() { - dst[i] = src[i].clone(); - } -} - #[warn(clippy::needless_range_loop)] pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) { // Same source and destination - don't trigger lint diff --git a/tests/ui/for_loop.stderr b/tests/ui/for_loop.stderr index 50f10d618..e8964c267 100644 --- a/tests/ui/for_loop.stderr +++ b/tests/ui/for_loop.stderr @@ -292,78 +292,5 @@ LL | vec.iter().cloned().map(|x| out.push(x)).collect::>(); | = note: `-D clippy::unused-collect` implied by `-D warnings` -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:363:14 - | -LL | for i in 0..src.len() { - | ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])` - | - = note: `-D clippy::manual-memcpy` implied by `-D warnings` - -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:368:14 - | -LL | for i in 0..src.len() { - | ^^^^^^^^^^^^ help: try replacing the loop by: `dst[10..(src.len() + 10)].clone_from_slice(&src[..])` - -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:373:14 - | -LL | for i in 0..src.len() { - | ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[10..])` - -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:378:14 - | -LL | for i in 11..src.len() { - | ^^^^^^^^^^^^^ help: try replacing the loop by: `dst[11..src.len()].clone_from_slice(&src[(11 - 10)..(src.len() - 10)])` - -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:383:14 - | -LL | for i in 0..dst.len() { - | ^^^^^^^^^^^^ help: try replacing the loop by: `dst.clone_from_slice(&src[..dst.len()])` - -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:396:14 - | -LL | for i in 10..256 { - | ^^^^^^^ -help: try replacing the loop by - | -LL | for i in dst[10..256].clone_from_slice(&src[(10 - 5)..(256 - 5)]) -LL | dst2[(10 + 500)..(256 + 500)].clone_from_slice(&src[10..256]) { - | - -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:408:14 - | -LL | for i in 10..LOOP_OFFSET { - | ^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[(10 + LOOP_OFFSET)..(LOOP_OFFSET + LOOP_OFFSET)].clone_from_slice(&src[(10 - some_var)..(LOOP_OFFSET - some_var)])` - -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:421:14 - | -LL | for i in 0..src_vec.len() { - | ^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst_vec[..src_vec.len()].clone_from_slice(&src_vec[..])` - -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:450:14 - | -LL | for i in from..from + src.len() { - | ^^^^^^^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + src.len()].clone_from_slice(&src[0..(from + src.len() - from)])` - -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:454:14 - | -LL | for i in from..from + 3 { - | ^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + 3].clone_from_slice(&src[0..(from + 3 - from)])` - -error: it looks like you're manually copying between slices - --> $DIR/for_loop.rs:461:14 - | -LL | for i in 0..src.len() { - | ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])` - -error: aborting due to 46 previous errors +error: aborting due to 35 previous errors diff --git a/tests/ui/manual_memcpy.rs b/tests/ui/manual_memcpy.rs new file mode 100644 index 000000000..aa3472888 --- /dev/null +++ b/tests/ui/manual_memcpy.rs @@ -0,0 +1,110 @@ +#![warn(clippy::needless_range_loop, clippy::manual_memcpy)] + +const LOOP_OFFSET: usize = 5000; + +pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) { + // plain manual memcpy + for i in 0..src.len() { + dst[i] = src[i]; + } + + // dst offset memcpy + for i in 0..src.len() { + dst[i + 10] = src[i]; + } + + // src offset memcpy + for i in 0..src.len() { + dst[i] = src[i + 10]; + } + + // src offset memcpy + for i in 11..src.len() { + dst[i] = src[i - 10]; + } + + // overwrite entire dst + for i in 0..dst.len() { + dst[i] = src[i]; + } + + // manual copy with branch - can't easily convert to memcpy! + for i in 0..src.len() { + dst[i] = src[i]; + if dst[i] > 5 { + break; + } + } + + // multiple copies - suggest two memcpy statements + for i in 10..256 { + dst[i] = src[i - 5]; + dst2[i + 500] = src[i] + } + + // this is a reversal - the copy lint shouldn't be triggered + for i in 10..LOOP_OFFSET { + dst[i + LOOP_OFFSET] = src[LOOP_OFFSET - i]; + } + + let some_var = 5; + // Offset in variable + for i in 10..LOOP_OFFSET { + dst[i + LOOP_OFFSET] = src[i - some_var]; + } + + // Non continuous copy - don't trigger lint + for i in 0..10 { + dst[i + i] = src[i]; + } + + let src_vec = vec![1, 2, 3, 4, 5]; + let mut dst_vec = vec![0, 0, 0, 0, 0]; + + // make sure vectors are supported + for i in 0..src_vec.len() { + dst_vec[i] = src_vec[i]; + } + + // lint should not trigger when either + // source or destination type is not + // slice-like, like DummyStruct + struct DummyStruct(i32); + + impl ::std::ops::Index for DummyStruct { + type Output = i32; + + fn index(&self, _: usize) -> &i32 { + &self.0 + } + } + + let src = DummyStruct(5); + let mut dst_vec = vec![0; 10]; + + for i in 0..10 { + dst_vec[i] = src[i]; + } + + // Simplify suggestion (issue #3004) + let src = [0, 1, 2, 3, 4]; + let mut dst = [0, 0, 0, 0, 0, 0]; + let from = 1; + + for i in from..from + src.len() { + dst[i] = src[i - from]; + } + + for i in from..from + 3 { + dst[i] = src[i - from]; + } +} + +#[warn(clippy::needless_range_loop, clippy::manual_memcpy)] +pub fn manual_clone(src: &[String], dst: &mut [String]) { + for i in 0..src.len() { + dst[i] = src[i].clone(); + } +} + +fn main() {} diff --git a/tests/ui/manual_memcpy.stderr b/tests/ui/manual_memcpy.stderr new file mode 100644 index 000000000..49ab83f2d --- /dev/null +++ b/tests/ui/manual_memcpy.stderr @@ -0,0 +1,75 @@ +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:7:14 + | +LL | for i in 0..src.len() { + | ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])` + | + = note: `-D clippy::manual-memcpy` implied by `-D warnings` + +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:12:14 + | +LL | for i in 0..src.len() { + | ^^^^^^^^^^^^ help: try replacing the loop by: `dst[10..(src.len() + 10)].clone_from_slice(&src[..])` + +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:17:14 + | +LL | for i in 0..src.len() { + | ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[10..])` + +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:22:14 + | +LL | for i in 11..src.len() { + | ^^^^^^^^^^^^^ help: try replacing the loop by: `dst[11..src.len()].clone_from_slice(&src[(11 - 10)..(src.len() - 10)])` + +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:27:14 + | +LL | for i in 0..dst.len() { + | ^^^^^^^^^^^^ help: try replacing the loop by: `dst.clone_from_slice(&src[..dst.len()])` + +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:40:14 + | +LL | for i in 10..256 { + | ^^^^^^^ +help: try replacing the loop by + | +LL | for i in dst[10..256].clone_from_slice(&src[(10 - 5)..(256 - 5)]) +LL | dst2[(10 + 500)..(256 + 500)].clone_from_slice(&src[10..256]) { + | + +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:52:14 + | +LL | for i in 10..LOOP_OFFSET { + | ^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[(10 + LOOP_OFFSET)..(LOOP_OFFSET + LOOP_OFFSET)].clone_from_slice(&src[(10 - some_var)..(LOOP_OFFSET - some_var)])` + +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:65:14 + | +LL | for i in 0..src_vec.len() { + | ^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst_vec[..src_vec.len()].clone_from_slice(&src_vec[..])` + +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:94:14 + | +LL | for i in from..from + src.len() { + | ^^^^^^^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + src.len()].clone_from_slice(&src[0..(from + src.len() - from)])` + +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:98:14 + | +LL | for i in from..from + 3 { + | ^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + 3].clone_from_slice(&src[0..(from + 3 - from)])` + +error: it looks like you're manually copying between slices + --> $DIR/manual_memcpy.rs:105:14 + | +LL | for i in 0..src.len() { + | ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])` + +error: aborting due to 11 previous errors +