Skip to content

Enum variants shadow associated constants with no warning #117558

Closed
@loynoir

Description

@loynoir

User Story

As a rust newbie, I don't know which one is recommended.

  • or impl FooEnum

  • or impl FooEnumWrapperStruct

So, I check doc

https://doc.rust-lang.org/stable/rust-by-example/custom_types/enum/testcase_linked_list.html

Found

  • official doc example use impl FooEnum, not impl FooEnumWrapperStruct

Curiously, I tried this code:

enum Foo {
    A,
    B,
    C,
}

impl Foo {
    pub const A: u8 = 42;
    pub const Z: u8 = 42;
}

fn main() {
    assert_eq!(Foo::A as u8, 0);
    assert_eq!(Foo::B as u8, 1);
    assert_eq!(Foo::C as u8, 2);
    assert_eq!(Foo::Z as u8, 42);
}

I expected to see this happen:

  • or same name is treated invalid: compile failure, pub const A conflicts with Foo:A

  • or same name is treated valid: there should be core::get_enum_impl_pub_const(Foo, "A") equals to 42

Instead, this happened:

warning: associated constant `A` is never used
 --> path/to/reproduce.rs:8:15
  |
7 | impl Foo {
  | -------- associated constant in this implementation
8 |     pub const A: u8 = 42;
  |               ^
  |
  = note: `#[warn(dead_code)]` on by default

Meta

rustc --version --verbose:

1.73.0
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions