-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Comments
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. |
New warnings usually require an RFC; I'm not sure if this could be an existing warning modified slightly. |
Related: I often get confused (momentarily...) by warnings about |
@jorendorff do you really think it's common that people use an existing variable name What do you think they are trying to do? |
@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? |
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)
Is this still true? |
According to the book, this is a common mistake (common enough to call out specifically, anyway):
Rust currently emits 3 warnings:
y
argument is unusedy
match-binding is unused_
pattern is unreachableAll 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 patterny
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 variabley
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.)
The text was updated successfully, but these errors were encountered: