Skip to content

Name of enum variant with fields is suggested in error messages #73427

Closed
@Aaron1011

Description

@Aaron1011

The following code:

enum MyEnum {
    First(bool),
    Second
}

fn main() {
    MyEnum.foo();
}

gives the following error message

error[E0423]: expected value, found enum `MyEnum`
 --> src/main.rs:7:5
  |
7 |     MyEnum.foo();
  |     ^^^^^^
  |
help: try using one of the enum's variants
  |
7 |     MyEnum::First.foo();
  |     ^^^^^^^^^^^^^
7 |     MyEnum::Second.foo();
  |     ^^^^^^^^^^^^^^

MyEnum::First.foo() calls foo on a function pointer (the constuctor function for MyEnum::First, which is almost never what you want.

At the very least, we should not be suggesting using enum variants that have associated data. When attempting to call a method on the base enum type, we might want to suggest using :: instead (e.g. MyEnum::First::foo()), which is more likely to be what the user wants.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions