Skip to content

Add hint to "use break" when attempting to implicit-break a loop #84114

Closed
@ash2x3zb9cy

Description

@ash2x3zb9cy

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6ad29d6923c9ffe687e2a08669a03101

fn main() {
    let x: i8 = loop { 10 };
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:2:24
  |
2 |     let x: i8 = loop { 10 };
  |                        ^^ expected `()`, found integer

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Ideally the output should look like:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:2:24
  |
2 |     let x: i8 = loop { 10 };
  |                       ^^^
  |                       | |
  |                       | expected `()`, found integer
  |                       help: consider adding `break` here
error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

It may not be immediately clear to a user why implicit-return/break style works for if expressions, but not for loop. Adding a hint to explicitly break with break value; would be helpful.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`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