rust-lang / rust-lang/rust-clippy

needless_borrow suggestion doesn't go far enough

Open
#8,389 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

In this code:

fn main() {
    let mut w = [42];
    (&mut w[..]).copy_from_slice(&[43]);
    println!("{:?}", &w);
}

clippy suggests as follows:

4 |     (&mut w[..]).copy_from_slice(&[43]);
  |     ^^^^^^^^^^^^ help: change this to: `w[..]`

This is correct as far as it goes. But a better suggestion would be just w, implying

    w.copy_from_slice(&[43]);

which also DTRT.

FTR, stable doesn't issue this lint in this situation at all. #8367 and #8355 may be relevant.

Version
rustcargo@zealot:/home/ian/Rustup/Arti/experiments$ rustc -Vv
rustc 1.60.0-nightly (a00e130da 2022-01-29)
binary: rustc
commit-hash: a00e130dae74a213338e2b095ec855156d8f3d8a
commit-date: 2022-01-29
host: x86_64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0
rustcargo@zealot:/home/ian/Rustup/Arti/experiments$
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 reproducing the needless_borrow example from the issue and inspect the lint's implementation and tests; no file or test path is named in the report. Compare the current suggestion with the requested w.copy_from_slice(&[43]) form, and review issues #8367 and #8355 for related context. Done means the lint suggests the shorter form where appropriate and its tests cover the behavior.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.