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

Suggest adding { .. } on accidental usage of unit variant pattern for a struct variant #126243

Closed
carols10cents opened this issue Jun 10, 2024 · 4 comments · Fixed by #129520
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@carols10cents
Copy link
Member

carols10cents commented Jun 10, 2024

Code

pub enum Error {
    NotFound,
    
    PreconditionFailed {
        description: String,
    }
}

pub fn demonstration(error: Error) {
    match error {
        Error::NotFound => println!("gone fishin"),
        Error::PreconditionFailed => println!("you're missing something"),
        _ => println!("i literally have no idea"),
    }
}

Current output

error[E0533]: expected unit struct, unit variant or constant, found struct variant `Error::PreconditionFailed`
  --> src/lib.rs:12:9
   |
12 |         Error::PreconditionFailed => println!("you're missing something"),
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^ not a unit struct, unit variant or constant

Desired output

error[E0533]: expected unit struct, unit variant or constant, found struct variant `Error::PreconditionFailed`
  --> src/lib.rs:12:9
   |
12 |         Error::PreconditionFailed => println!("you're missing something"),
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^ not a unit struct, unit variant or constant
help: if you don't care about the variant's fields, ignore them with `{ .. }`
   |
12 |         Error::PreconditionFailed { .. } => println!("you're missing something"),
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Rationale and extra context

playground

I wrote code that looked like this while not having my brain turned on 100%. Due to said brain not functioning, it took me a while to understand what rustc was trying to tell me with the message pointing to the Error::PreconditionFailed pattern with the text not a unit struct, unit variant or constant. Error::PreconditionFailed sure looked like a unit variant to me!

What rustc was trying to tell me was that the definition of the Error::PreconditionFailed variant was a struct variant ("found struct variant") and my pattern wasn't matching the definition ("this pattern you specified looks like it's trying to match a variant that is not a unit struct, unit variant or constant").

The change I needed to make was adding { .. } to my pattern, because I was trying to distinguish different variants from each other, not use pieces within the variants. Suggesting { .. } seems like a decent starting point to me, but there could definitely be cases I'm not considering here and I'm totally open to hearing that ❤️

Other cases

No response

Rust Version

$ rustc --version --verbose
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: aarch64-apple-darwin
release: 1.78.0
LLVM version: 18.1.2

Anything else?

No response

@carols10cents carols10cents added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 10, 2024
@carols10cents
Copy link
Member Author

WEIRD this PR looks like it implements what I wish rustc did: #64660

but the test files in that PR say they're for error[E0532] and I'm getting error[E0533]

@carols10cents
Copy link
Member Author

Aha ok this test shows that for tuple variants, rustc will suggest adding (_):

| ^^^^^^^^^^^^^ help: use the tuple variant pattern syntax instead: `MyEnum::Tuple(_)`

and that test also shows that rustc isn't suggesting anything for struct variants!

@carols10cents
Copy link
Member Author

Aha and it looks like adding struct fields was more complex so that's why it wasn't done? #63983 (comment)

@jieyouxu jieyouxu added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. labels Jun 11, 2024
@chenyukang chenyukang assigned chenyukang and unassigned chenyukang Jul 3, 2024
@tunawasabi
Copy link
Contributor

@rustbot claim

workingjubilee added a commit to workingjubilee/rustc that referenced this issue Sep 11, 2024
…attern-syntax, r=compiler-errors

Suggest the correct pattern syntax on usage of unit variant pattern for a struct variant

Closes rust-lang#126243

I add a suggestion on usage of unit variant pattern for a struct variant.
@bors bors closed this as completed in 1d6edee Sep 11, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 11, 2024
Rollup merge of rust-lang#129520 - tunawasabi:suggest-adding-struct-pattern-syntax, r=compiler-errors

Suggest the correct pattern syntax on usage of unit variant pattern for a struct variant

Closes rust-lang#126243

I add a suggestion on usage of unit variant pattern for a struct variant.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants