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

manual_memcpy false positive #9334

Closed
ViridiFox opened this issue Aug 14, 2022 · 2 comments · Fixed by #12010
Closed

manual_memcpy false positive #9334

ViridiFox opened this issue Aug 14, 2022 · 2 comments · Fixed by #12010
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@ViridiFox
Copy link

ViridiFox commented Aug 14, 2022

Summary

Copying the diagonal from a 2d Matrix/Vec to a 1d Vec gives the false positive that it can be replaced by a copy_from_slice.

Lint Name

manual_memcpy

Reproducer

the minimal code:

fn main() {
    let m2d = vec![vec![0; 10]; 10];
    let mut m1d = vec![0; 10];

    for i in 0..10 {
        m1d[i] = m2d[i][i];
    }
}

gives the warning:

warning: it looks like you're manually copying between slices
 --> src/main.rs:5:5
  |
5 | /     for i in 0..10 {
6 | |         m1d[i] = m2d[i][i];
7 | |     }
  | |_____^ help: try replacing the loop by: `m1d[..10].copy_from_slice(&m2d[i][..10]);`
  |
  = note: `#[warn(clippy::manual_memcpy)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy```

Version

rustc 1.65.0-nightly (d394408fb 2022-08-07)
binary: rustc
commit-hash: d394408fb38c4de61f765a3ed5189d2731a1da91
commit-date: 2022-08-07
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 14.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

@ViridiFox ViridiFox added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Aug 14, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Aug 14, 2022
@Bubbler-4
Copy link

A similar false positive is triggered by a situation in reverse.

fn main() {
    let mut m2d = vec![vec![0; 10]; 10];
    let mut m1d = vec![0; 10];

    for i in 0..10 {
        m1d[i] = m2d[i][i];
    }

    for i in 0..10 {
        m2d[i][i] = m1d[i];
    }
}
warning: it looks like you're manually copying between slices
 --> src/main.rs:5:5
  |
5 | /     for i in 0..10 {
6 | |         m1d[i] = m2d[i][i];
7 | |     }
  | |_____^ help: try replacing the loop by: `m1d[..10].copy_from_slice(&m2d[i][..10]);`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy
  = note: `#[warn(clippy::manual_memcpy)]` on by default

warning: it looks like you're manually copying between slices
  --> src/main.rs:9:5
   |
9  | /     for i in 0..10 {
10 | |         m2d[i][i] = m1d[i];
11 | |     }
   | |_____^ help: try replacing the loop by: `m2d[i][..10].copy_from_slice(&m1d[..10]);`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy

warning: `playground` (bin "playground") generated 2 warnings

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=bc438205758c5e8f5caf6f4707a76533

@granddaifuku
Copy link
Contributor

@rustbot claim

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 I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants