rust-lang / rust-lang/rust-clippy

False positive for `enum_clike_unportable_variant` for values that are variable based on the width of a `usize`

Open
#8,043 5 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Lint name:
enum_clike_unportable_variant

I tried this code:

#[repr(usize)]
pub enum Foo {
    Bar = 0,
    Baz = HIGH_BIT | 1,
}

const HIGH_BIT: usize = 1usize.rotate_right(1);

I expected to see this happen:
Since the value of HIGH_BIT is variable dependent on the size of usize, it should never overflow a usize.
That code is portable to 32-bit architectures because when compiled for a 32-bit arch, the value of HIGH_BIT would change to fit within a 32-width usize

Instead, this happened:

error: C-like enum variant discriminant is not portable to 32-bit targets
 --> src/lib.rs:4:5
  |
4 |     Baz = HIGH_BIT | 1,
  |     ^^^^^^^^^^^^^^^^^^
  |
  = note: `#[deny(clippy::enum_clike_unportable_variant)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#enum_clike_unportable_variant

Meta

Rust version (rustc -Vv):

rustc 1.58.0-nightly (a77da2d45 2021-11-19)
binary: rustc
commit-hash: a77da2d454e6caa227a85b16410b95f93495e7e0
commit-date: 2021-11-19
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

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

Start with the enum_clike_unportable_variant lint and reproduce the report using the example in src/lib.rs, comparing behavior across usize widths. Done means the width-dependent discriminant is no longer reported as non-portable on 32-bit targets while genuinely overflowing values remain diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.