rust-lang / rust-lang/rust-clippy

manual_memcpy should not suggest addition in

Open
#11,688 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

The manual_memcpy lint makes suggestions that are not always the easiest to read when iterating with a offset.

Reproducer

For example:

    let src: &[u8] = todo!();
    let dest: &mut [u8] = todo!();
    
    for i in 0..4 {
        dest[i] = src[1 + i];
    }

Leads to a suggestion to use instead:

dest[..4].copy_from_slice(&src[1..(4 + 1)]);

Which works, but in this specific case, I would probably manually write:

dest[..4].copy_from_slice(&src[1..][..4]);

Which is more readable. src[1..5] would also make sense but it seems harder to generalize and implement, and introduces a number with a non-obvious source.

Version
rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the manual_memcpy lint and reproducing the reported src[1 + i] case from the issue. Trace how the slice suggestion is formed, then verify that offset iteration produces a readable alternative without changing other cases; the reproducer should no longer suggest 4 + 1 in the resulting range.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.