rust-lang / rust-lang/rustfmt

Semicolon after a return statement breaks code with try blocks and never type

Open
#6,553 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Consider this code:

#![feature(never_type, try_blocks)]

fn main() {
    let _: Result<!, ()> = try {
        ();
        return
    };
}

It compiles.

Rustfmt adds a semicolon here:

#![feature(never_type, try_blocks)]

fn main() {
    let _: Result<!, ()> = try {
        ();
        return;
    };
}

This causes a compilation error:

   Compiling playground v0.0.1 (/playground)
error[E0271]: type mismatch resolving `<Result<!, ()> as Try>::Output == ()`
 --> src/main.rs:7:5
  |
7 |     };
  |     ^ expected `()`, found `!`
  |
  = note: expected unit type `()`
                  found type `!`

For more information about this error, try `rustc --explain E0271`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Playground link

Previously reported here.

Meta
Nightly channel

Build using the Nightly version: 1.88.0-nightly

(2025-04-26 10fa3c449f6b1613b352)

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

Start by reproducing the two snippets from the issue with the linked Rust Playground and compare rustfmt's output with rustc's result. The payload names no repository file or test entry point, so locate the formatting path and existing try-block tests in rustfmt. Done means formatting this case no longer changes its compilation behavior, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.