Skip to content

Add Default Lint to Ensure Match Arm Bindings do not Shadow Local Variables. #68433

Open
@Alexhuszagh

Description

@Alexhuszagh

Given the following code:

fn foo(x: u32) -> u32 {
  let a = 100;
  let b = 200;
  let c = 300;

  match x {
      c => 3,
      b => 2,
      a => 1,
      _ => 0
  }
}

we currently emit:

warning: unreachable pattern
 --> <source>:8:7
  |
7 |       c => 3,
  |       - matches any value
8 |       b => 2,
  |       ^ unreachable pattern
  |
  = note: `#[warn(unreachable_patterns)]` on by default

However, in this case, the developer was likely confused and thought they were matching on the value of c, not creating a binding of with name c. We should emit something more similar to:

warning: match arm binding shadows local binding
 --> <source>:8:7
  |
7 |       c => 3,
  |       ^
  = note: shadows local binding `c` declared at <source>:4:7
  = note: `#[warn(arm_binding_shadows_local)]` on by default

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions