Skip to content

uninhabited_references warns on common patterns like match *x {} #11985

Closed as not planned
@Kixunil

Description

@Kixunil

Summary

It's very common to match on uninhabited types when we statically know they are uninhabited. It's a very common pattern to express that the code is known to be dead. Especially trait impls of uninhabited types. Aside from that I believe the whole lint is broken since dereferencing an uninhabited type is not UB - it cannot happen in the first place. Creating it is UB. The lint should instead disallow casting inhabited types to uninhabited (using transmute or pointers).

Lint Name

uninhabited_references

Reproducer

I tried this code:

enum Foo {}

fn never_happens(foo: &Foo) {
    match *foo {}
}

I saw this happen:

warning: dereferencing a reference to an uninhabited type is undefined behavior
 --> src/lib.rs:4:11
  |
4 |     match *foo {}
  |           ^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninhabited_references
  = note: `#[warn(clippy::uninhabited_references)]` on by default

I expected to see this happen: No warning

Version

rustc 1.76.0-nightly (3f28fe133 2023-12-18)
binary: rustc
commit-hash: 3f28fe133475ec5faf3413b556bf3cfb0d51336c
commit-date: 2023-12-18
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions