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

diagnostics: better warning in case of let declaration and assignment operator: let y += 4 #54143

Closed
matthiaskrgr opened this issue Sep 11, 2018 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

fn main() {
    let y = 3;
    let y = y + 4;

this code compiles fine, but if I try to use something like let y += 4; rustc has no idea what is going on:

fn main() {
    let y = 3;
    let y += 4;
}
    Compiling mul v0.1.0 (file:///tmp/mul)
error: expected one of `:`, `;`, `=`, or `@`, found `+=`
 --> src/main.rs:3:11
  |
3 |     let y += 4;
  |           ^^ expected one of `:`, `;`, `=`, or `@` here
error: aborting due to previous error

I wonder if we could figure out that the user wants let y = y + 4 in this case and suggest it?

@memoryruins
Copy link
Contributor

memoryruins commented Sep 12, 2018

Instead of shadowing, I would first think they meant to type y += 4; without the let and to make the first y mutable.

@memoryruins
Copy link
Contributor

memoryruins commented Sep 12, 2018

But if that’s assuming too much, then I think your suggestion is better than nothing.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 12, 2018
@estebank estebank added A-parser Area: The parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 18, 2019
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@JohnTitor
Copy link
Member

The current output:

error: can't reassign to an uninitialized variable
 --> src/main.rs:3:11
  |
3 |     let y += 4;
  |           ^^ help: initialize the variable
  |
  = help: if you meant to overwrite, remove the `let` binding

The diagnostics have improved by #83062 and others. Closing as 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-parser Area: The parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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