rust-lang / rust-lang/rust-clippy

Use & in pattern binding to remove noisy dereferencing of copy types.

Open
#4,062 0 comments 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

The following:

fn main() {
    let data = vec![1, 2, 3, 4, 5];
    for i in &data {
        if *i == 3 {
            println!("Hmm!");
        }
        if *i == 2 {
            println!("Ahh?");
        }
        if *i == 1 {
            println!("Wut?!");
        }
    }
}

is less noisily expressed as:

fn main() {
    let data = vec![1, 2, 3, 4, 5];
    for &i in &data {
        if i == 3 {
            println!("Hmm!");
        }
        if i == 2 {
            println!("Ahh?");
        }
        if i == 1 {
            println!("Wut?!");
        }
    }
}

and it's obviously shorter than .iter().cloned().

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 Clippy lint entry point and test structure for Rust pattern-related suggestions, then compare them with the examples in this issue. Done should include a lint that recognizes the noisy dereferencing form, suggests the & pattern binding, and verifies the shown cases without changing unrelated code.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.