Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DjLogozzo opened this issue Nov 30, 2021 · 4 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@DjLogozzo
Copy link

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
@DjLogozzo DjLogozzo added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 30, 2021
@DjLogozzo
Copy link
Author

Possibly related issue: #1134
The issue I'm having looks like its related to this issue, where the value computed for the enum value is not taking into account that the value might be different on other architectures

@DjLogozzo
Copy link
Author

DjLogozzo commented Nov 30, 2021

When running clippy with a 32-bit target triple, it no-longer errors out

64-bit Architecture

$ cargo clippy --target x86_64-unknown-linux-gnu
    Checking enum-test v0.1.0 (/home/user/src/enum-test)
error: C-like enum variant discriminant is not portable to 32-bit targets
 --> src/main.rs:6:5
  |
6 |     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

error: could not compile `enum-test` due to previous error

32-bit Architecture

$ cargo clippy --target i686-unknown-linux-gnu
    Checking enum-test v0.1.0 (/home/user/src/enum-test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s

@dimpolo
Copy link

dimpolo commented Dec 10, 2021

Minimized:

#[repr(usize)]
enum Foo {
    A = usize::MAX,
}

@tobz
Copy link

tobz commented Aug 15, 2024

Just a fellow traveler stopping by to report that this false positive is still present, nearly three years later. 😓

(Rust 1.80, using the very same minimized example.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

3 participants