- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.8k
          Expands manual_memcpy to lint ones with loop counters
          #5727
        
          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
Changes from all commits
dc89bb1
              116f30d
              b2d5b89
              31cb110
              c599e2f
              13c207d
              9573a0d
              1026b42
              b4b4da1
              720f19f
              de56279
              8da6cfd
              d9a88be
              774e82a
              9aad38b
              4ea4a97
              eb3ffe6
              10d7a18
              174065f
              4418738
              f410df3
              ce653d6
              e855fe3
              4918e7a
              5c71352
              99aceeb
              9725f00
              ec94bd6
              3883841
              94d7b82
              1402d8a
              41a0ccb
              2a0e45b
              7820cb1
              b541884
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
This file was deleted.
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #![warn(clippy::needless_range_loop, clippy::manual_memcpy)] | ||
|  | ||
| pub fn manual_copy_with_counters(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) { | ||
| let mut count = 0; | ||
| for i in 3..src.len() { | ||
| dst[i] = src[count]; | ||
| count += 1; | ||
| } | ||
|  | ||
| let mut count = 0; | ||
| for i in 3..src.len() { | ||
| dst[count] = src[i]; | ||
| count += 1; | ||
| } | ||
|  | ||
| let mut count = 3; | ||
| for i in 0..src.len() { | ||
| dst[count] = src[i]; | ||
| count += 1; | ||
| } | ||
|  | ||
| let mut count = 3; | ||
| for i in 0..src.len() { | ||
| dst[i] = src[count]; | ||
| count += 1; | ||
| } | ||
|  | ||
| let mut count = 0; | ||
| for i in 3..(3 + src.len()) { | ||
| dst[i] = src[count]; | ||
| count += 1; | ||
| } | ||
|  | ||
| let mut count = 3; | ||
| for i in 5..src.len() { | ||
| dst[i] = src[count - 2]; | ||
| count += 1; | ||
| } | ||
|  | ||
| let mut count = 2; | ||
| for i in 0..dst.len() { | ||
| dst[i] = src[count]; | ||
| count += 1; | ||
| } | ||
|  | ||
| let mut count = 5; | ||
| for i in 3..10 { | ||
| dst[i] = src[count]; | ||
| count += 1; | ||
| } | ||
|  | ||
| let mut count = 3; | ||
| let mut count2 = 30; | ||
| for i in 0..src.len() { | ||
| dst[count] = src[i]; | ||
| dst2[count2] = src[i]; | ||
| count += 1; | ||
| count2 += 1; | ||
| } | ||
|  | ||
| // make sure parentheses are added properly to bitwise operators, which have lower precedence than | ||
| // arithmetric ones | ||
| let mut count = 0 << 1; | ||
| for i in 0..1 << 1 { | ||
| dst[count] = src[i + 2]; | ||
| count += 1; | ||
| } | ||
|  | ||
| // make sure incrementing expressions without semicolons at the end of loops are handled correctly. | ||
| let mut count = 0; | ||
| for i in 3..src.len() { | ||
| dst[i] = src[count]; | ||
| count += 1 | ||
| } | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be the last review round: I would like two more tests (correct me if they won't make sense): 
 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I probably thought it was fine because the  | ||
|  | ||
| // make sure ones where the increment is not at the end of the loop. | ||
| // As a possible enhancement, one could adjust the offset in the suggestion according to | ||
| // the position. For example, if the increment is at the top of the loop; | ||
| // treating the loop counter as if it were initialized 1 greater than the original value. | ||
| let mut count = 0; | ||
| #[allow(clippy::needless_range_loop)] | ||
| for i in 0..src.len() { | ||
| count += 1; | ||
| dst[i] = src[count]; | ||
| } | ||
| } | ||
|  | ||
| fn main() {} | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| 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 | | dst[i] = src[count]; | ||
| LL | | count += 1; | ||
| LL | | } | ||
| | |_____^ help: try replacing the loop by: `dst[3..src.len()].clone_from_slice(&src[..(src.len() - 3)]);` | ||
| | | ||
| = note: `-D clippy::manual-memcpy` implied by `-D warnings` | ||
|  | ||
| error: it looks like you're manually copying between slices | ||
| --> $DIR/with_loop_counters.rs:11:5 | ||
| | | ||
| LL | / for i in 3..src.len() { | ||
| LL | | dst[count] = src[i]; | ||
| LL | | count += 1; | ||
| LL | | } | ||
| | |_____^ help: try replacing the loop by: `dst[..(src.len() - 3)].clone_from_slice(&src[3..]);` | ||
|  | ||
| error: it looks like you're manually copying between slices | ||
| --> $DIR/with_loop_counters.rs:17:5 | ||
| | | ||
| LL | / for i in 0..src.len() { | ||
| LL | | dst[count] = src[i]; | ||
| LL | | count += 1; | ||
| LL | | } | ||
| | |_____^ help: try replacing the loop by: `dst[3..(src.len() + 3)].clone_from_slice(&src[..]);` | ||
|  | ||
| error: it looks like you're manually copying between slices | ||
| --> $DIR/with_loop_counters.rs:23:5 | ||
| | | ||
| LL | / for i in 0..src.len() { | ||
| LL | | dst[i] = src[count]; | ||
| LL | | count += 1; | ||
| LL | | } | ||
| | |_____^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[3..(src.len() + 3)]);` | ||
|  | ||
| error: it looks like you're manually copying between slices | ||
| --> $DIR/with_loop_counters.rs:29:5 | ||
| | | ||
| LL | / for i in 3..(3 + src.len()) { | ||
| LL | | dst[i] = src[count]; | ||
| LL | | count += 1; | ||
| LL | | } | ||
| | |_____^ help: try replacing the loop by: `dst[3..((3 + src.len()))].clone_from_slice(&src[..((3 + src.len()) - 3)]);` | ||
|  | ||
| error: it looks like you're manually copying between slices | ||
| --> $DIR/with_loop_counters.rs:35:5 | ||
| | | ||
| LL | / for i in 5..src.len() { | ||
| LL | | dst[i] = src[count - 2]; | ||
| LL | | count += 1; | ||
| LL | | } | ||
| | |_____^ help: try replacing the loop by: `dst[5..src.len()].clone_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 | ||
| | | ||
| LL | / for i in 0..dst.len() { | ||
| LL | | dst[i] = src[count]; | ||
| LL | | count += 1; | ||
| LL | | } | ||
| | |_____^ help: try replacing the loop by: `dst.clone_from_slice(&src[2..(dst.len() + 2)]);` | ||
|  | ||
| error: it looks like you're manually copying between slices | ||
| --> $DIR/with_loop_counters.rs:47:5 | ||
| | | ||
| LL | / for i in 3..10 { | ||
| LL | | dst[i] = src[count]; | ||
| LL | | count += 1; | ||
| LL | | } | ||
| | |_____^ help: try replacing the loop by: `dst[3..10].clone_from_slice(&src[5..(10 + 5 - 3)]);` | ||
|  | ||
| error: it looks like you're manually copying between slices | ||
| --> $DIR/with_loop_counters.rs:54:5 | ||
| | | ||
| LL | / for i in 0..src.len() { | ||
| LL | | dst[count] = src[i]; | ||
| LL | | dst2[count2] = src[i]; | ||
| LL | | count += 1; | ||
| LL | | count2 += 1; | ||
| LL | | } | ||
| | |_____^ | ||
| | | ||
| help: try replacing the loop by | ||
| | | ||
| LL | dst[3..(src.len() + 3)].clone_from_slice(&src[..]); | ||
| LL | dst2[30..(src.len() + 30)].clone_from_slice(&src[..]); | ||
| | | ||
|  | ||
| error: it looks like you're manually copying between slices | ||
| --> $DIR/with_loop_counters.rs:64:5 | ||
| | | ||
| LL | / for i in 0..1 << 1 { | ||
| LL | | dst[count] = src[i + 2]; | ||
| LL | | count += 1; | ||
| LL | | } | ||
| | |_____^ help: try replacing the loop by: `dst[(0 << 1)..((1 << 1) + (0 << 1))].clone_from_slice(&src[2..((1 << 1) + 2)]);` | ||
|  | ||
| error: it looks like you're manually copying between slices | ||
| --> $DIR/with_loop_counters.rs:71:5 | ||
| | | ||
| LL | / for i in 3..src.len() { | ||
| LL | | dst[i] = src[count]; | ||
| LL | | count += 1 | ||
| LL | | } | ||
| | |_____^ help: try replacing the loop by: `dst[3..src.len()].clone_from_slice(&src[..(src.len() - 3)]);` | ||
|  | ||
| error: aborting due to 11 previous errors | ||
|  | 
Uh oh!
There was an error while loading. Please reload this page.