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

Enum variants shadow associated constants with no warning #117558

Open
loynoir opened this issue Nov 3, 2023 · 1 comment
Open

Enum variants shadow associated constants with no warning #117558

loynoir opened this issue Nov 3, 2023 · 1 comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@loynoir
Copy link

loynoir commented Nov 3, 2023

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>

@loynoir loynoir added the C-bug Category: This is a bug. label Nov 3, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 3, 2023
@clubby789
Copy link
Contributor

clubby789 commented Nov 3, 2023

Same issue as #76347 I think. Seems to be intended given #57501, but it's confusing not to have a lint

@clubby789 clubby789 changed the title when impl FooEnum have same name Enum variants shadow associated constants Nov 3, 2023
@clubby789 clubby789 added C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 3, 2023
@clubby789 clubby789 changed the title Enum variants shadow associated constants Enum variants shadow associated constants with no warning Nov 3, 2023
@fmease fmease added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants