rust-lang / rust-lang/rust-clippy

Unable to allow clippy::similar_names

Open
#9,514 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I am unable to disable (allow) the clippy::similar_names lint the way I expect. Usually I would place #[allow(clippy::x)] before the offending line, and the lint error would go away. In this case the allow seems to have no effect.

For example, this code shows the lint error being given despite being allowed on the testb line (playground):

#![deny(clippy::pedantic)]

fn main() {
    let testa = 0u32;
    #[allow(clippy::similar_names)]
    let testb = 0u32;
    println!("{testa}, {testb}"); // Use variables
}

error: binding's name is too similar to existing binding
 --> src/main.rs:6:9
  |
6 |     let testb = 0u32;
  |         ^^^^^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(clippy::pedantic)]
  |         ^^^^^^^^^^^^^^^^
  = note: `#[deny(clippy::similar_names)]` implied by `#[deny(clippy::pedantic)]`
note: existing binding defined here
 --> src/main.rs:4:9
  |
4 |     let testa = 0u32;
  |         ^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#similar_names

I expected that allowing on the testb line would hide the error since that's the line referenced by the error message. To cover my bases, I can also put the allow on testa with the same result (playground):

#![deny(clippy::pedantic)]

fn main() {
    #[allow(clippy::similar_names)]
    let testa = 0u32;
    #[allow(clippy::similar_names)]
    let testb = 0u32;
    println!("{testa}, {testb}"); // Use variables
}

What does work is allowing on the containing function (playground). But of course this prevents other instances from being caught so is not an ideal workaround.

#![deny(clippy::pedantic)]

#[allow(clippy::similar_names)]
fn main() {
    let testa = 0u32;
    let testb = 0u32;
    println!("{testa}, {testb}"); // Use variables
}

I found this on stable 1.63 but also seems to occur on beta and nightly.

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

Reproduce the minimal Rust example from the issue, using the linked Playground cases to compare line-level and function-level allowances. Trace the similar_names lint entry point and its reported spans and lint levels. Done means a targeted allow suppresses only the intended diagnostic while other similar_names warnings remain enabled.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.