-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Beginners are often confused by the pattern where a set of types are combined using an enum
, especially when the name of the enum variant is the same as the name of the variant's field's type. Even knowing how it works, I've myself forgotten to write out enum variant patterns containing struct patterns where they are needed. It would be useful if the compiler could give suggestions that understand this mistake.
Given the following code:
enum Foo {
Bar(Bar),
}
struct Bar {
x: i32,
}
fn oops_1(f: Foo) {
match f {
Foo::Bar { x } => todo!()
}
}
fn oops_2(f: Foo) {
match f {
Bar { x } => todo!()
}
}
The current output is:
error[E0769]: tuple variant `Foo::Bar` written as struct variant
--> src/lib.rs:10:9
|
10 | Foo::Bar { x } => todo!()
| ^^^^^^^^^^^^^^
|
help: use the tuple variant pattern syntax instead
|
10 | Foo::Bar(x) => todo!()
| ~~~
error[E0308]: mismatched types
--> src/lib.rs:15:9
|
14 | match f {
| - this expression has type `Foo`
15 | Bar { x } => todo!()
| ^^^^^^^^^ expected enum `Foo`, found struct `Bar`
The compiler could instead recognize these cases, and produce the suggestion Foo::Bar(Bar { x }) =>
as well as a message specific to the situation:
- A variant pattern with fields which do not match the variant, but do match the fields of the variant's sole field.
- A pattern not of enum type where an enum pattern is expected, but the enum has a variant whose sole field is of the pattern's type.
@rustbot label +A-suggestion-diagnostics +C-enhancement +D-newcomer-roadblock
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.