Skip to content

Rustc suggest break with value inside while #150850

Closed

Description

@Exonorid

Code

pub fn a() {
    let a = while false {
        some_function()
    };
}

fn some_function() -> i32 {
    1
}

Current output

error[E0308]: mismatched types
 --> <source>:3:9
  |
2 |       let a = while false {
  |  _____________-
3 | |         some_function()
  | |         ^^^^^^^^^^^^^^^ expected `()`, found `i32`
4 | |     };
  | |_____- expected this to be `()`
  |
help: consider using a semicolon here
  |
3 |         some_function();
  |                        +
help: consider using a semicolon here
  |
4 |     };;
  |      +
help: you might have meant to break the loop with this value
  |
3 |         break some_function();
  |         +++++                +

Desired output

error[E0308]: mismatched types
 --> <source>:3:9
  |
2 |       let a = while false {
  |  _____________-
3 | |         some_function()
  | |         ^^^^^^^^^^^^^^^ expected `()`, found `i32`
4 | |     };
  | |_____- `while` loops can't produce a non-trivial (i.e. not `()`) value
  |
help: consider using a semicolon here
  |
3 |         some_function();
  |                        +

Rationale and extra context

The suggestion to add a second semicolon after the loop is evidently wrong, and the suggestion to replace the expression with a break expression may mislead the user into thinking that will solve the problem. I can see how it may be debatable how likely confusion from said second point is, but the compiler knows from the fact that it's in a while loop that adding break won't do anything, so I don't see a good reason not to change it.

Other cases

If the expression inside the block is not a function call, the suggestions to add a semicolon disappear.

Rust Version

rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3

Anything else?

No response

Activity

  1. 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 Jan 9, 2026
  2. added
    D-confusingDiagnostics: Confusing error or lint that should be reworked.
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    on Jan 9, 2026
  3. eggyal commented on Jan 9, 2026

    @eggyal
    Contributor

    @rustbot claim

  4. added a commit that references this issue on Jan 10, 2026
    86806ab
  5. added a commit that references this issue on Jan 11, 2026
    915b4eb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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