-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Merged
Merged
Changes from 28 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
dc89bb1
Use if_chain in Increment/InitializeVisitor
rail-rain 116f30d
Use else blocks instead of return statements in Increment/InitializeV…
rail-rain b2d5b89
Check if it's after the loop earlier
rail-rain 31cb110
add concinient methods to Increment/InitializeVisitor
rail-rain c599e2f
Split VarState
rail-rain 13c207d
Generalise `InitializeVisitor`
rail-rain 9573a0d
Rename variables
rail-rain 1026b42
Rename a struct and variables
rail-rain b4b4da1
Introduce Start and StartKind
rail-rain 720f19f
Implement detecting `manual_memcpy` with loop counters
rail-rain de56279
Implement building the `manual_memcpy` sugggestion with loop counters
rail-rain 8da6cfd
fmt
rail-rain d9a88be
Rename `get_offset` and its private items
rail-rain 774e82a
Add the tests for `manual_memcpy` with loop counters
rail-rain 9aad38b
Update `manual_memcpy.stderr` to reflect additional parentheses
rail-rain 4ea4a97
Add tests for bitwise operations
rail-rain eb3ffe6
make use of macros in operator overloading
rail-rain 10d7a18
fmt
rail-rain 174065f
fix the multiple counters test
rail-rain 4418738
Use operator overloading instead of direct calls of `make_binop`
rail-rain f410df3
make clippy happy (`needless_pass_by_value`, `filter_map` and `find_m…
rail-rain ce653d6
use `#[derive]` instead of the manual implementation
rail-rain e855fe3
Reflect the changes that has been made and fmt
rail-rain 4918e7a
Replace `snippet_opt` + `unwrap_or_else` with `snippet`
rail-rain 5c71352
Prevent unnecessary lints from triggering
rail-rain 99aceeb
Use the spans of the entire `for` loops for suggestions
rail-rain 9725f00
Use the `From` trait to make `MinifyingSugg`
rail-rain ec94bd6
split up the `manual_memcpy` test
rail-rain 3883841
document `MinifyingSugg` and `Offset`
rail-rain 94d7b82
simplify the code
rail-rain 1402d8a
fix a FN where incr exprs with no semicolon at ends
rail-rain 41a0ccb
add comments around `loop_counters`
rail-rain 2a0e45b
supress `clippy::filter_map`
rail-rain 7820cb1
Add tests for
rail-rain b541884
remove the explicit return value of `print_limit`
rail-rain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#![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 = 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; | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
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 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:48: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:58: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: aborting due to 9 previous errors | ||
|
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.