diff options
Diffstat (limited to 'src/tools/clippy/tests/ui/manual_memcpy')
4 files changed, 78 insertions, 24 deletions
diff --git a/src/tools/clippy/tests/ui/manual_memcpy/with_loop_counters.rs b/src/tools/clippy/tests/ui/manual_memcpy/with_loop_counters.rs index c826b082a..786d7e6e2 100644 --- a/src/tools/clippy/tests/ui/manual_memcpy/with_loop_counters.rs +++ b/src/tools/clippy/tests/ui/manual_memcpy/with_loop_counters.rs @@ -1,50 +1,59 @@ #![warn(clippy::needless_range_loop, clippy::manual_memcpy)] - +//@no-rustfix pub fn manual_copy_with_counters(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) { let mut count = 0; for i in 3..src.len() { + //~^ ERROR: it looks like you're manually copying between slices + //~| NOTE: `-D clippy::manual-memcpy` implied by `-D warnings` dst[i] = src[count]; count += 1; } let mut count = 0; for i in 3..src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[count] = src[i]; count += 1; } let mut count = 3; for i in 0..src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[count] = src[i]; count += 1; } let mut count = 3; for i in 0..src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[count]; count += 1; } let mut count = 0; for i in 3..(3 + src.len()) { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[count]; count += 1; } let mut count = 3; for i in 5..src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[count - 2]; count += 1; } let mut count = 2; for i in 0..dst.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[count]; count += 1; } let mut count = 5; for i in 3..10 { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[count]; count += 1; } @@ -52,6 +61,7 @@ pub fn manual_copy_with_counters(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) let mut count = 3; let mut count2 = 30; for i in 0..src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[count] = src[i]; dst2[count2] = src[i]; count += 1; @@ -62,6 +72,7 @@ pub fn manual_copy_with_counters(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) // arithmetic ones let mut count = 0 << 1; for i in 0..1 << 1 { + //~^ ERROR: it looks like you're manually copying between slices dst[count] = src[i + 2]; count += 1; } @@ -69,6 +80,7 @@ pub fn manual_copy_with_counters(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) // make sure incrementing expressions without semicolons at the end of loops are handled correctly. let mut count = 0; for i in 3..src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[count]; count += 1 } diff --git a/src/tools/clippy/tests/ui/manual_memcpy/with_loop_counters.stderr b/src/tools/clippy/tests/ui/manual_memcpy/with_loop_counters.stderr index 79d40c0bc..3f000fbab 100644 --- a/src/tools/clippy/tests/ui/manual_memcpy/with_loop_counters.stderr +++ b/src/tools/clippy/tests/ui/manual_memcpy/with_loop_counters.stderr @@ -2,80 +2,91 @@ error: it looks like you're manually copying between slices --> $DIR/with_loop_counters.rs:5:5 | LL | / for i in 3..src.len() { +LL | | +LL | | LL | | dst[i] = src[count]; LL | | count += 1; LL | | } | |_____^ help: try replacing the loop by: `dst[3..src.len()].copy_from_slice(&src[..(src.len() - 3)]);` | = note: `-D clippy::manual-memcpy` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::manual_memcpy)]` error: it looks like you're manually copying between slices - --> $DIR/with_loop_counters.rs:11:5 + --> $DIR/with_loop_counters.rs:13:5 | LL | / for i in 3..src.len() { +LL | | LL | | dst[count] = src[i]; LL | | count += 1; LL | | } | |_____^ help: try replacing the loop by: `dst[..(src.len() - 3)].copy_from_slice(&src[3..]);` error: it looks like you're manually copying between slices - --> $DIR/with_loop_counters.rs:17:5 + --> $DIR/with_loop_counters.rs:20:5 | LL | / for i in 0..src.len() { +LL | | LL | | dst[count] = src[i]; LL | | count += 1; LL | | } | |_____^ help: try replacing the loop by: `dst[3..(src.len() + 3)].copy_from_slice(&src[..]);` error: it looks like you're manually copying between slices - --> $DIR/with_loop_counters.rs:23:5 + --> $DIR/with_loop_counters.rs:27:5 | LL | / for i in 0..src.len() { +LL | | LL | | dst[i] = src[count]; LL | | count += 1; LL | | } | |_____^ help: try replacing the loop by: `dst[..src.len()].copy_from_slice(&src[3..(src.len() + 3)]);` error: it looks like you're manually copying between slices - --> $DIR/with_loop_counters.rs:29:5 + --> $DIR/with_loop_counters.rs:34:5 | LL | / for i in 3..(3 + src.len()) { +LL | | LL | | dst[i] = src[count]; LL | | count += 1; LL | | } | |_____^ help: try replacing the loop by: `dst[3..(3 + src.len())].copy_from_slice(&src[..(3 + src.len() - 3)]);` error: it looks like you're manually copying between slices - --> $DIR/with_loop_counters.rs:35:5 + --> $DIR/with_loop_counters.rs:41:5 | LL | / for i in 5..src.len() { +LL | | LL | | dst[i] = src[count - 2]; LL | | count += 1; LL | | } | |_____^ help: try replacing the loop by: `dst[5..src.len()].copy_from_slice(&src[(3 - 2)..((src.len() - 2) + 3 - 5)]);` error: it looks like you're manually copying between slices - --> $DIR/with_loop_counters.rs:41:5 + --> $DIR/with_loop_counters.rs:48:5 | LL | / for i in 0..dst.len() { +LL | | LL | | dst[i] = src[count]; LL | | count += 1; LL | | } | |_____^ help: try replacing the loop by: `dst.copy_from_slice(&src[2..(dst.len() + 2)]);` error: it looks like you're manually copying between slices - --> $DIR/with_loop_counters.rs:47:5 + --> $DIR/with_loop_counters.rs:55:5 | LL | / for i in 3..10 { +LL | | LL | | dst[i] = src[count]; LL | | count += 1; LL | | } | |_____^ help: try replacing the loop by: `dst[3..10].copy_from_slice(&src[5..(10 + 5 - 3)]);` error: it looks like you're manually copying between slices - --> $DIR/with_loop_counters.rs:54:5 + --> $DIR/with_loop_counters.rs:63:5 | LL | / for i in 0..src.len() { +LL | | LL | | dst[count] = src[i]; LL | | dst2[count2] = src[i]; LL | | count += 1; @@ -90,18 +101,20 @@ LL + dst2[30..(src.len() + 30)].copy_from_slice(&src[..]); | error: it looks like you're manually copying between slices - --> $DIR/with_loop_counters.rs:64:5 + --> $DIR/with_loop_counters.rs:74:5 | LL | / for i in 0..1 << 1 { +LL | | LL | | dst[count] = src[i + 2]; LL | | count += 1; LL | | } | |_____^ help: try replacing the loop by: `dst[(0 << 1)..((1 << 1) + (0 << 1))].copy_from_slice(&src[2..((1 << 1) + 2)]);` error: it looks like you're manually copying between slices - --> $DIR/with_loop_counters.rs:71:5 + --> $DIR/with_loop_counters.rs:82:5 | LL | / for i in 3..src.len() { +LL | | LL | | dst[i] = src[count]; LL | | count += 1 LL | | } diff --git a/src/tools/clippy/tests/ui/manual_memcpy/without_loop_counters.rs b/src/tools/clippy/tests/ui/manual_memcpy/without_loop_counters.rs index 4d5c70f19..a224001a3 100644 --- a/src/tools/clippy/tests/ui/manual_memcpy/without_loop_counters.rs +++ b/src/tools/clippy/tests/ui/manual_memcpy/without_loop_counters.rs @@ -1,31 +1,37 @@ #![warn(clippy::needless_range_loop, clippy::manual_memcpy)] #![allow(clippy::useless_vec)] - +//@no-rustfix 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() { + //~^ ERROR: it looks like you're manually copying between slices + //~| NOTE: `-D clippy::manual-memcpy` implied by `-D warnings` dst[i] = src[i]; } // dst offset memcpy for i in 0..src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[i + 10] = src[i]; } // src offset memcpy for i in 0..src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[i + 10]; } // src offset memcpy for i in 11..src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[i - 10]; } // overwrite entire dst for i in 0..dst.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[i]; } @@ -39,6 +45,7 @@ pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) { // multiple copies - suggest two memcpy statements for i in 10..256 { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[i - 5]; dst2[i + 500] = src[i] } @@ -51,6 +58,7 @@ pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) { let some_var = 5; // Offset in variable for i in 10..LOOP_OFFSET { + //~^ ERROR: it looks like you're manually copying between slices dst[i + LOOP_OFFSET] = src[i - some_var]; } @@ -64,6 +72,7 @@ pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) { // make sure vectors are supported for i in 0..src_vec.len() { + //~^ ERROR: it looks like you're manually copying between slices dst_vec[i] = src_vec[i]; } @@ -93,20 +102,24 @@ pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) { let from = 1; for i in from..from + src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[i - from]; } for i in from..from + 3 { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[i - from]; } #[allow(clippy::identity_op)] for i in 0..5 { + //~^ ERROR: it looks like you're manually copying between slices dst[i - 0] = src[i]; } #[allow(clippy::reversed_empty_ranges)] for i in 0..0 { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[i]; } @@ -130,6 +143,7 @@ pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) { #[warn(clippy::needless_range_loop, clippy::manual_memcpy)] pub fn manual_clone(src: &[String], dst: &mut [String]) { for i in 0..src.len() { + //~^ ERROR: it looks like you're manually copying between slices dst[i] = src[i].clone(); } } diff --git a/src/tools/clippy/tests/ui/manual_memcpy/without_loop_counters.stderr b/src/tools/clippy/tests/ui/manual_memcpy/without_loop_counters.stderr index 1c6a7d5c0..b9dbda6ed 100644 --- a/src/tools/clippy/tests/ui/manual_memcpy/without_loop_counters.stderr +++ b/src/tools/clippy/tests/ui/manual_memcpy/without_loop_counters.stderr @@ -2,48 +2,56 @@ error: it looks like you're manually copying between slices --> $DIR/without_loop_counters.rs:8:5 | LL | / for i in 0..src.len() { +LL | | +LL | | LL | | dst[i] = src[i]; LL | | } | |_____^ help: try replacing the loop by: `dst[..src.len()].copy_from_slice(&src[..]);` | = note: `-D clippy::manual-memcpy` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::manual_memcpy)]` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:13:5 + --> $DIR/without_loop_counters.rs:15:5 | LL | / for i in 0..src.len() { +LL | | LL | | dst[i + 10] = src[i]; LL | | } | |_____^ help: try replacing the loop by: `dst[10..(src.len() + 10)].copy_from_slice(&src[..]);` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:18:5 + --> $DIR/without_loop_counters.rs:21:5 | LL | / for i in 0..src.len() { +LL | | LL | | dst[i] = src[i + 10]; LL | | } | |_____^ help: try replacing the loop by: `dst[..src.len()].copy_from_slice(&src[10..(src.len() + 10)]);` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:23:5 + --> $DIR/without_loop_counters.rs:27:5 | LL | / for i in 11..src.len() { +LL | | LL | | dst[i] = src[i - 10]; LL | | } | |_____^ help: try replacing the loop by: `dst[11..src.len()].copy_from_slice(&src[(11 - 10)..(src.len() - 10)]);` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:28:5 + --> $DIR/without_loop_counters.rs:33:5 | LL | / for i in 0..dst.len() { +LL | | LL | | dst[i] = src[i]; LL | | } | |_____^ help: try replacing the loop by: `dst.copy_from_slice(&src[..dst.len()]);` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:41:5 + --> $DIR/without_loop_counters.rs:47:5 | LL | / for i in 10..256 { +LL | | LL | | dst[i] = src[i - 5]; LL | | dst2[i + 500] = src[i] LL | | } @@ -56,57 +64,64 @@ LL + dst2[(10 + 500)..(256 + 500)].copy_from_slice(&src[10..256]); | error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:53:5 + --> $DIR/without_loop_counters.rs:60:5 | LL | / for i in 10..LOOP_OFFSET { +LL | | LL | | dst[i + LOOP_OFFSET] = src[i - some_var]; LL | | } | |_____^ help: try replacing the loop by: `dst[(10 + LOOP_OFFSET)..(LOOP_OFFSET + LOOP_OFFSET)].copy_from_slice(&src[(10 - some_var)..(LOOP_OFFSET - some_var)]);` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:66:5 + --> $DIR/without_loop_counters.rs:74:5 | LL | / for i in 0..src_vec.len() { +LL | | LL | | dst_vec[i] = src_vec[i]; LL | | } | |_____^ help: try replacing the loop by: `dst_vec[..src_vec.len()].copy_from_slice(&src_vec[..]);` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:95:5 + --> $DIR/without_loop_counters.rs:104:5 | LL | / for i in from..from + src.len() { +LL | | LL | | dst[i] = src[i - from]; LL | | } | |_____^ help: try replacing the loop by: `dst[from..(from + src.len())].copy_from_slice(&src[..(from + src.len() - from)]);` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:99:5 + --> $DIR/without_loop_counters.rs:109:5 | LL | / for i in from..from + 3 { +LL | | LL | | dst[i] = src[i - from]; LL | | } | |_____^ help: try replacing the loop by: `dst[from..(from + 3)].copy_from_slice(&src[..(from + 3 - from)]);` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:104:5 + --> $DIR/without_loop_counters.rs:115:5 | LL | / for i in 0..5 { +LL | | LL | | dst[i - 0] = src[i]; LL | | } | |_____^ help: try replacing the loop by: `dst[..5].copy_from_slice(&src[..5]);` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:109:5 + --> $DIR/without_loop_counters.rs:121:5 | LL | / for i in 0..0 { +LL | | LL | | dst[i] = src[i]; LL | | } | |_____^ help: try replacing the loop by: `dst[..0].copy_from_slice(&src[..0]);` error: it looks like you're manually copying between slices - --> $DIR/without_loop_counters.rs:132:5 + --> $DIR/without_loop_counters.rs:145:5 | LL | / for i in 0..src.len() { +LL | | LL | | dst[i] = src[i].clone(); LL | | } | |_____^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..]);` |