Skip to content

UI test cleanup: Extract manual_memcpy tests #3880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 0 additions & 108 deletions tests/ui/for_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,114 +355,6 @@ fn partition<T: PartialOrd + Send>(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<usize> 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
Expand Down
75 changes: 1 addition & 74 deletions tests/ui/for_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -292,78 +292,5 @@ LL | vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
|
= 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

110 changes: 110 additions & 0 deletions tests/ui/manual_memcpy.rs
Original file line number Diff line number Diff line change
@@ -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<usize> 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() {}
75 changes: 75 additions & 0 deletions tests/ui/manual_memcpy.stderr
Original file line number Diff line number Diff line change
@@ -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