Skip to content

E0499 error shouldn't suggest adding a semicolon when there already is one #133941

Closed
@josephcsible

Description

@josephcsible

Code

use std::marker::PhantomData;

struct Bar<'a>(PhantomData<&'a mut i32>);

impl<'a> Drop for Bar<'a> {
    fn drop(&mut self) {}
}

struct Foo();

impl Foo {
    fn f(&mut self) -> Option<Bar<'_>> {
        None
    }
    
    fn g(&mut self) {}
}

fn main() {
    let mut foo = Foo();
    while let Some(_) = foo.f() {
        foo.g();
    };
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0499]: cannot borrow `foo` as mutable more than once at a time
  --> src/main.rs:22:9
   |
21 |     while let Some(_) = foo.f() {
   |                         -------
   |                         |
   |                         first mutable borrow occurs here
   |                         a temporary with access to the first borrow is created here ...
22 |         foo.g();
   |         ^^^ second mutable borrow occurs here
23 |     };
   |     - ... and the first borrow might be used here, when that temporary is dropped and runs the destructor for type `Option<Bar<'_>>`
   |
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
   |
23 |     };;
   |      +

For more information about this error, try `rustc --explain E0499`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

Not have the help section since there already is a semicolon

Rationale and extra context

No response

Other cases

Rust Version

Tested on the Rust Playground with both stable 1.83.0 and 1.85.0-nightly 2024-12-04 acabb52 (sorry for the lack of the exact command you wanted, but see rust-lang/rust-playground#1043)

Anything else?

No response

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Dec 5, 2024
added
D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
and removed
D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
on Dec 6, 2024
josephcsible

josephcsible commented on Dec 18, 2024

@josephcsible
Author

Not sure if it's in-scope of this issue or should be a separate one, but would it be feasible for it to suggest this?

    loop {
        let Some(_) = foo.f() else { break };
        foo.g();
    };
added 2 commits that reference this issue on Feb 2, 2025

Rollup merge of rust-lang#136402 - notriddle:notriddle/let-expr-detec…

858eb47

Rollup merge of rust-lang#136402 - notriddle:notriddle/let-expr-detec…

e066208
added a commit that references this issue on Feb 2, 2025
f3cb010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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

      Participants

      @josephcsible@workingjubilee

      Issue actions

        E0499 error shouldn't suggest adding a semicolon when there already is one · Issue #133941 · rust-lang/rust