Skip to content

while let suggestion missing when E0308 happens in closure body #113354

Closed
@e2-71828

Description

@e2-71828

Code

fn main() {
    let _ = || { while Some(_) = None { } };
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:2:24
  |
2 |     let _ = || { while Some(_) = None { } };
  |                        ^^^^^^^^^^^^^^ expected `bool`, found `()`

For more information about this error, try `rustc --explain E0308`.

Desired output

error[E0308]: mismatched types
 --> src/main.rs:2:24
  |
2 |     let _ = || { while Some(_) = None { } };
  |                        ^^^^^^^^^^^^^^ expected `bool`, found `()`
help: consider adding `let`
  |
2 |     let _ = || { while let Some(_) = None { } };
  |                        +++

For more information about this error, try `rustc --explain E0308`.

Rationale and extra context

When an assignment is used as the test in a while statement, the compiler will add a help message suggesting while let instead. This message fails to appear when the erroneous while statement is inside a closure body.

Other cases

No response

Anything else?

This problem was discovered while investigating #113352 .

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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