Skip to content

default_numeric_fallback triggers on enums with an explicit repr #9656

@RReverser

Description

@RReverser

Summary

default_numeric_fallback triggers on enums with a specified repr(...). The values are already checked by the compiler against the specified representation type and can't fallback into another type.

Might be similar to #9632, but I'm guessing will require a separate fix.

Lint Name

default_numeric_fallback

Reproducer

I tried this code:

#![warn(clippy::default_numeric_fallback)]

#[repr(i32)]
pub enum State {
    Error = -1,
    Idle = 0,
    Waiting = 1,
    Done = 2,
}

I saw this happen:

warning: default numeric fallback might occur
 --> src/lib.rs:5:14
  |
5 |     Error = -1,
  |              ^ help: consider adding suffix: `1_i32`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_numeric_fallback
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::default_numeric_fallback)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: default numeric fallback might occur
 --> src/lib.rs:6:12
  |
6 |     Idle = 0,
  |            ^ help: consider adding suffix: `0_i32`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_numeric_fallback

warning: default numeric fallback might occur
 --> src/lib.rs:7:15
  |
7 |     Waiting = 1,
  |               ^ help: consider adding suffix: `1_i32`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_numeric_fallback

warning: default numeric fallback might occur
 --> src/lib.rs:8:12
  |
8 |     Done = 2,
  |            ^ help: consider adding suffix: `2_i32`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_numeric_fallback

warning: `playground` (lib) generated 4 warnings

I expected to see this happen:

(no warning)

Version

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-pc-windows-msvc
release: 1.64.0
LLVM version: 14.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