rust-lang / rust-lang/rust-clippy

Match Allows Multiple Identical as i32 Statements Without Warning

Open
#14,427 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

I tried this code:

#[repr(i32)]
pub enum Something {
    Foo = 0,
    Bar = 1,
    Blarg = 2,
}

fn main() {
    let my_int: i32 = 1;

    match my_int {
        1 => {}
        //1 => {} // Warning: no value can reach this
        x if x == Something::Foo as i32 => {}
        x if x == Something::Foo as i32 => {} // No warning
        x if x == Something::Foo as i32 => {} // No warning
        x if x == Something::Blarg as i32 => {}
        _ => {}
    }
}

rustc seems to happily accept multiple identical cases of enums as i32 in match statements. Using i32 directly gives warnings as expected, but not when representing the enum as i32. There are no warnings in nightly nor in rustc 1.0.0, tested on https://rust.godbolt.org/.

I would expect the same warning for the second x if x == Something::Foo as i32 => {} as for the second 1 => {} in the example below.

At the time of writing https://rust.godbolt.org/ does not allow me to see the disassembly for nightly. But for 1.0.0 the same code is generated for each statement with different offsets.

rustc --version --verbose:

nightly

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

No repository file or test path is named. Start by reproducing the Rust match example with the cited rustc versions and tracing the relevant match-analysis entry point; done means duplicate guarded enum-as-i32 cases produce the expected warning with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
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.