-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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.3Anything else?
No response
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.