rust-lang / rust-lang/rust

unreachable code is not detected if it depends on never type fallback

Open
#146,085 1 comment 0 reactions 2 assignees View on GitHub

@jdonszelmann is already working on this.

Since May 6, 2026.

A-lints C-bug F-never_type L-false-negative L-unreachable_code S-has-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

fn main() {
    (|| panic!())();
    ();
}

I expected to see this happen: (); is marked as unreachable code

Instead, this happened: there is no warning.

This happens because we emit unreachable code lint in rustc_hir_typeck, while checking expressions (we track it via FnCtxt::diverges), which means that we can have unresolved inference variables.

In this case, when checking code reachibility we see that the type of (|| panic!())() is a (diverging) inference variable and not ! so we don't set FnCtxt::diverges to "always". Later this inference variable is set to ! (NB: currently only on edition = 2024), but at this point we don't check for unreachable code.

Meta

rustc --version --verbose:

1.91.0-nightly (2025-08-31 07d246fc6dc227903da2)

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.