Skip to content

Explicit match on uninhabited type shouldn't warn #55116

Closed
@RalfJung

Description

@RalfJung

So say I have a function that takes an uninhabited type (common e.g. when implementing a trait for !). I would like to make it explicit that I am using "ex falso quod libet" reasoning, so I write

fn foo(x: !) -> bool {
  match x {}
}

Unfortunately, now Rust warns:

warning: unreachable expression
 --> src/lib.rs:4:3
  |
4 |   match x {}
  |   ^^^^^^^^^^
  |
  = note: #[warn(unreachable_code)] on by default

It seems it wants me to instead write

fn foo(x: !) -> bool {
    x
}

I think that's bad style, now I have to think much harder to figure out what is going on ("oh, there's a coercion from uninhabited types to bool"). The original code I wrote uses a construct which only works for uninhabited types, explicitly documenting intent.

I think the compiler should not print a warning for that code. Empty matches are a useful, explicit and constructive way of saying that we are dealing with uninhabited data, we shouldn't discourage them in favor of implicit coercions that require way more context to comprehend.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions