Skip to content

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

Closed
@matthiaskrgr

Description

@matthiaskrgr
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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions