rust-lang / rust-lang/rust-analyzer

Shadowed variable is highlighted in cfg'ed out code even though it is not used there

Open
#20,216 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-highlighting C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: 0.3.2533-standalone

rustc version: rustc 1.88.0 (6b00bc388 2025-06-23)

editor or extension: Code OSS (like vscode)

relevant settings: None

code snippet to reproduce:

pub fn f() {
    if let Some(var) = Some(123) {
        let var = var + 1;

        #[cfg(debug_assertions)]
        dbg!(var);
        #[cfg(not(debug_assertions))]
        let _ = var;
    }
}

When the cursor is on any uses of var others are highlighted, but the use that is cfg'ed out (debug_assertions is on) highlights and is highlighted by the shadowed var, not the new let var = ... later on.

Image Image

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 with the minimal Rust snippet in the issue and trace rust-analyzer's semantic highlighting and cfg handling for the shadowed var. Reproduce the highlighting with debug_assertions enabled, then verify that the cfg-disabled use no longer affects or receives the wrong binding highlight.

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.