Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Alexhuszagh opened this issue Jan 21, 2020 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Alexhuszagh
Copy link
Contributor

Alexhuszagh commented Jan 21, 2020

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
@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 21, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jan 21, 2020
@Programistich
Copy link

Hello from vas3k tech

@VCasecnikovs
Copy link

All guys, who tried rust had this problem. One guy even left Rust in favor of elixir because of this.

@dvozzhaev
Copy link

This is really confusing. Any chance having this fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants