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 lints against slices but not pointer arythmetics #13859

Open
sosthene-nitrokey opened this issue Dec 20, 2024 · 0 comments
Open
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@sosthene-nitrokey
Copy link

Summary

manual_memcpy should be able to suggest using core::ptr::copy_nonoverlapping when relevant

Lint Name

manual_memcpy

Reproducer

I tried this code:

#![allow(unreachable_code)]

fn main() {
    let p: *const u32 = todo!();
    let mut dest = [0u32; 10];
    
    for i in 0..10 {
        dest[i] = unsafe {*p.add(i)};
    }
}

I expected to see this happen:

Clippy should suggest to replace it with the following (maybe using core::ptr::copy if clippy can't assert from type information (such as one being a mutable reference) that the pointers don't overlap).

#![allow(unreachable_code)]

fn main() {
    let p: *const u32 = todo!();
    let mut dest = [0u32; 10];
    
    unsafe {
        core::ptr::copy_nonoverlapping(p, dest.as_mut_ptr(), 10);
    }
}

Instead, this happened:

The cilppy lints needless_range_loops was triggered:

warning: the loop variable `i` is used to index `dest`
 --> src/main.rs:7:14
  |
7 |     for i in 0..10 {
  |              ^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop
  = note: `#[warn(clippy::needless_range_loop)]` on by default
help: consider using an iterator and enumerate()
  |
7 |     for (i, <item>) in dest.iter_mut().enumerate() {
  |         ~~~~~~~~~~~    ~~~~~~~~~~~~~~~~~~~~~~~~~~~

warning: `playground` (bin "playground") generated 2 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.58s

Version

Nightly channel

Build using the Nightly version: 1.85.0-nightly

(2024-12-19 9e136a30a965bf4e63f0)
@sosthene-nitrokey sosthene-nitrokey added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Dec 20, 2024
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-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

No branches or pull requests

1 participant