Skip to content

manual_memcpy lints against slices but not pointer arythmetics #13859

Open
@sosthene-nitrokey

Description

@sosthene-nitrokey

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions