rust-lang / rust-lang/rust

unhelpful suggestion: consider using a semicolon here

Open
#122,155 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-suggestion-diagnostics D-invalid-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Code
fn f() -> Result<u8, ()> {
    match true {
        true => {},
        _ => {g()?},
    }
    g()
}

fn g() -> Result<u8, ()> {unimplemented!()}
Current output
error[E0308]: mismatched types
 --> src/lib.rs:4:15
  |
2 | /     match true {
3 | |         true => {},
4 | |         _ => {g()?},
  | |               ^^^^ expected `()`, found `u8`
5 | |     }
  | |_____- expected this to be `()`
  |
help: consider using a semicolon here
  |
4 |         _ => {g()?;},
  |                   +
help: consider using a semicolon here
  |
5 |     };
  |      +

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

This suggestion should not be be displayed because the first suggestion is the correct one. This second suggestion does not fix the compile error and makes the next compile error worse.

help: consider using a semicolon here
  |
5 |     };
  |      +
Rationale and extra context

If the second suggestion is applied, the code still doesn't compile. Even worse, it no longer suggests the first (correct) fix if the second one has been applied. This is the output after adding the semicolon on line 5:

Other cases
error[E0308]: `match` arms have incompatible types
 --> src/lib.rs:4:15
  |
2 | /     match true {
3 | |         true => {},
  | |                 -- this is found to be of type `()`
4 | |         _ => {g()?},
  | |               ^^^^ expected `()`, found `u8`
5 | |     };
  | |_____- `match` arms have incompatible types

For more information about this error, try `rustc --explain E0308`.
Rust Version
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic using the Rust code in src/lib.rs and the reported rustc 1.76.0 version. Then trace the compiler logic that emits the E0308 semicolon suggestions and add or adjust coverage for this match-expression case. Done means the incorrect second suggestion is no longer displayed while the valid inner semicolon suggestion remains.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.