-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[Proposal] Prevent compilation of assignment of assignment #1227
Comments
While the behaviour is indeed interesting (result of assignment expression is Perhaps a lint would be better; it looks somewhat footgunny because result of assignment expression in most other languages is the assigned value. |
It may lead to some inadequate errors when working with generics |
A lint would be a good idea here; a hard error would introduce a weird inconsistency to Rust's semantics. Possibly a special note could be appended if an assignment expression fails to typecheck (directly or as a variable created by a |
Multiple assignments are unavailable in Rust, because only one variable must be an owner. So this code doesn't make a sense in any situation. Also this is error-prone. Why should this be allowed? Only to make Rust's semantics be consistent? It is weird. Rust aims to eliminate many errors. This is one of those errors. |
First of all, making this code not compile would be a backwards incompatible change, so that's really not an option. This is a perfectly valid way of assigning An assignment is an expression that evaluates another expression and assigns its value to a variable declared by a The only way this would be possible would be to change the type of value an assignment evaluates to, and to change assignments to take any expression that does not evaluate to that type (similar to the way that However, again, this change is not backward compatible. Adding a warning is. |
@withoutboats I am not sure code contains this exists. And also if that code is exists, doesn't this mean that code contains an error? |
@KalitaAlexey : first, the assignment doesn't need to be enclosed in parens. The problem is that the only sensible way to differentiate between expressions during the static analysis phase of Rust is by the type of the expressions. Currently, assignments evaluate to So some sort of lint seems much more sensible here. It could identify any assignment of an assignment to issue a warning that assignments evaluate to I do think that it would be better if assignments evaluated to |
@withoutboats Oh. I have figured out. I have though that Rust's analyzer can determine case where right-side of assignment is another assignment. |
While I agree that if we were to do something like this it would fit better as a lint than as an error, I don't see how the lint would end up being helpful. I can't think of a non-trivial case where assigning the result of an assignment to a variable by accident would compile without either type checking errors or unused variable warnings. If you can think of convincing a non-trivial case however, then you can propose this functionality in rust-clippy. |
It's not so much that it would compile as that new users may be confused about why it does not compile, because they are used to languages with multiple assignment. |
Now compiles, but it is should not.
The text was updated successfully, but these errors were encountered: