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

suggestion for unit enum variant when matched with a patern #84818

Merged
merged 1 commit into from
May 3, 2021

Conversation

ABouttefeux
Copy link
Contributor

resolve #84700

add suggestion for code like

enum FarmAnimal {
    Worm,
    Cow,
    Bull,
    Chicken { num_eggs: usize },
    Dog (String),
}

fn what_does_the_animal_say(animal: &FarmAnimal) {

    let noise = match animal {
        FarmAnimal::Cow(_) => "moo".to_string(),
        _ => todo!()
    };

    println!("{:?} says: {:?}", animal, noise);
}
error[E0532]: expected tuple struct or tuple variant, found unit variant `FarmAnimal::Cow`
  --> $DIR/issue-84700.rs:15:9
   |
LL |     Cow,
   |     --- `FarmAnimal::Cow` defined here
...
LL |         FarmAnimal::Cow(_) => "moo".to_string(),
   |         ^^^^^^^^^^^^^^^^^^ help: use this syntax instead: `FarmAnimal::Cow`

@rust-highfive
Copy link
Collaborator

r? @jackh726

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 2, 2021
@jackh726
Copy link
Member

jackh726 commented May 2, 2021

LGTM. Thanks!

@bors r+ rollup

@bors
Copy link
Contributor

bors commented May 2, 2021

📌 Commit 5cc21d9 has been approved by jackh726

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 2, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request May 3, 2021
Rollup of 6 pull requests

Successful merges:

 - rust-lang#84072 (Allow setting `target_family` to multiple values, and implement `target_family="wasm"`)
 - rust-lang#84744 (Add ErrorKind::OutOfMemory)
 - rust-lang#84784 (Add help message to suggest const for unused type param)
 - rust-lang#84811 (RustDoc: Fix bounds linking trait.Foo instead of traitalias.Foo)
 - rust-lang#84818 (suggestion for unit enum variant when matched with a patern)
 - rust-lang#84832 (Do not print visibility in external traits)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b0c7e64 into rust-lang:master May 3, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 3, 2021
span,
&format!("use this syntax instead"),
format!("{path_str}"),
Applicability::MaybeIncorrect,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be machine applicable? In what cases it will be incorrect?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, there are two different suggestions that get emitted, so there could be some ambiguity. (I guess the "help: a tuple struct with a similar name exists" suggestion might just never be what the user wants, but that decision would have to be made in another PR)

@ABouttefeux ABouttefeux deleted the enum-suggest branch May 16, 2021 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Diagnostics: Suggest Unit Variant Enum in match
6 participants