Skip to content

match on a ! variable reports unused variable warning #158783

Description

@ds84182

I tried this code:

fn foo() -> ! { todo!() }

fn main() {
    let res = foo();
    match res as Result<u32, u32> {
        Ok(v) => todo!(),
        Err(err) => todo!(),
    }
}

I expected to see this happen: Should only have an unreachable code warning.

Instead, this happened: Also says res is unused.

warning: unreachable expression
 --> src/main.rs:5:5
  |
4 |       let res = foo();
  |                 ----- any code following this expression is unreachable
5 | /     match res as Result<u32, u32> {
6 | |         Ok(v) => todo!(),
7 | |         Err(err) => todo!(),
8 | |     }
  | |_____^ unreachable expression
  |
  = note: `#[warn(unreachable_code)]` (part of `#[warn(unused)]`) on by default

warning: unused variable: `res`
 --> src/main.rs:4:9
  |
4 |     let res = foo();
  |         ^^^ help: if this is intentional, prefix it with an underscore: `_res`
  |
  = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

Meta

rustc --version --verbose:

1.96.1

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-unused_variablesLint: unused_variablesT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions