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

Consider specific warning for trying to use an existing variable in a pattern #40476

Open
jorendorff opened this issue Mar 13, 2017 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jorendorff
Copy link
Contributor

According to the book, this is a common mistake (common enough to call out specifically, anyway):

fn compare(x: i32, y: i32) {
    match x {
        y => println!("match"),   // wrong
        _ => println!("no match")
    }
}

fn main() { compare(3, 4); }

Rust currently emits 3 warnings:

  • y argument is unused
  • y match-binding is unused
  • _ pattern is unreachable

All are correct. But if this really is a common pitfall, it'd better for Rust to have a specific diagnostic for this problem.

The last two warnings, in combination with the other binding y already in scope, strongly suggest that the programmer has made this exact mental mistake. The third warning in particular shows that the programmer expected the pattern y to be refutable.

The new warning could be something like: "the identifier y in this pattern introduces a new variable (it does not refer to the variable y declared here)"

(Of course shadowing an existing binding is normal in other circumstances; I'm only proposing a different warning for certain situations where Rust already emits warnings.)

@jorendorff
Copy link
Contributor Author

jorendorff commented Mar 13, 2017

The reason I bring this up is that I'm writing about this "possible pitfall" in Programming Rust, spending about half a page on it. To me that expenditure is regrettable. If Rust had a really great diagnostic for this, I wouldn't mention it at all.

@steveklabnik steveklabnik added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 13, 2017
@steveklabnik
Copy link
Member

New warnings usually require an RFC; I'm not sure if this could be an existing warning modified slightly.

@nikomatsakis
Copy link
Contributor

Related: I often get confused (momentarily...) by warnings about _ being unreachable because I've failed to import the name of some variant, but this seems to some extent to be because rustc still doesn't use Enum::Variant much of the time (but instead imports Variant directly into scope).

@nikomatsakis
Copy link
Contributor

@jorendorff do you really think it's common that people use an existing variable name What do you think they are trying to do?

@jorendorff
Copy link
Contributor Author

@nikomatsakis I don't think it's super common. I probably wouldn't have thought of it if TRPL didn't mention it. @steveklabnik Do you know where that passage came from? Is this really a common thing?

Anyway, what I imagine they're trying to do is match the exact value that's in the variable. Note that that's exactly what happens if you use the name of a constant in a pattern.

Elixir patterns have a "pin operator" that does this, so apparently it's something people want to do... sometimes?

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@BGR360
Copy link
Contributor

BGR360 commented Dec 29, 2021

The book no longer contains any mention of this pitfall (as far as I can tell), but I could certainly understand people still stumbling over this. (for posterity, here's the section of the old book that talked about the pitfall)

New warnings usually require an RFC

Is this still true?

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

5 participants