Skip to content
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

Misleading manual_memcpy suggestion #3004

Closed
rizakrko opened this issue Aug 4, 2018 · 1 comment
Closed

Misleading manual_memcpy suggestion #3004

rizakrko opened this issue Aug 4, 2018 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@rizakrko
Copy link

rizakrko commented Aug 4, 2018

Following code:

fn copy(arr: &mut [u8], from: usize, target: &[u8]) {
    for i in from..from + target.len() {
        arr[i] = target[i - from];
    }
}

triggers this warning:

warning: it looks like you're manually copying between slices
 --> src/main.rs:2:14
  |
2 |     for i in from..from + target.len() {
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing the loop by: `arr[from..from + target.len()].clone_from_slice(&target[(from - from)..(from + target.len() - from)])`
  |
  = note: #[warn(manual_memcpy)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#manual_memcpy

But this contains (from - from), which by itself leads to next error:

error: equal expressions as operands to `-`
 --> src/main.rs:3:35
  |
3 |         .clone_from_slice(&target[(from - from)..(from + target.len() - from)])
  |                                   ^^^^^^^^^^^^^
  |
  = note: #[deny(eq_op)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#eq_op

In the case of copying the entire slice clippy may provide specialized suggestion

warning: it looks like you're manually copying between slices
 --> src/main.rs:2:14
  |
2 |     for i in from..from + target.len() {
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing the loop by: `arr[from..from + target.len()].clone_from_slice(&target)`
  |
@phansch phansch added C-bug Category: Clippy is not doing the correct thing L-suggestion Lint: Improving, adding or fixing lint suggestions labels Aug 6, 2018
@ghost
Copy link

ghost commented Aug 7, 2018

Related to #2828

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

2 participants