rust-lang / rust-lang/rust-clippy

redundant_clone: false negative with unneeded ref

Open
#7,227 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Lint name:

I tried this code:

fn opt_string() -> Option<String> {
    Some(String::from("hello world"))
}

fn main() {
    let s: String = match opt_string() {
        Some(ref string) => string.clone(), // we take a ref here so we have to clone()
        // but actually  opt_string() returns an owned value and we could consume it
        _ => String::new(),
    };

    // this could be:

    let _s_better: String = match opt_string()  {
        Some(string) => string,
        _ => String::new(),
    };
    
    assert_eq!("hello world".to_string(), s);
}
Meta
  • cargo clippy -V: clippy 0.1.54 (8cf990c 2021-05-15)
  • rustc -Vv:

rustc 1.54.0-nightly (8cf990c9b 2021-05-15)
binary: rustc
commit-hash: 8cf990c9b5c59f25c806fad9f4466f9d6509bbea
commit-date: 2021-05-15
host: x86_64-unknown-linux-gnu
release: 1.54.0-nightly
LLVM version: 12.0.1

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 Rust snippet with the redundant_clone lint enabled through cargo clippy. Compare the reported behavior with the ownership shown in the example, then locate the lint implementation and its existing tests. Done means the false negative is covered by a regression test and the lint correctly identifies the unnecessary reference and clone.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.