Skip to content

Diagnostics: Suggest Unit Variant Enum in match #84700

Closed
@jamesmunns

Description

@jamesmunns

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5a6b1a3497842e4ddc883b2345febd8f

#[derive(PartialEq, Debug)]
enum FarmAnimal {
    Worm,
    Cow,
    Bull,
    Chicken { num_eggs: usize },
    Dog { name: String },
}

fn what_does_the_animal_say(animal: &FarmAnimal) {

    /* TODO: fill in the match statement below to make this code compile */
    
    let noise = match animal {
        FarmAnimal::Cow(_) => "moo".to_string(),
        // /* Chicken      */ => "cluck, cluck!".to_string(),
        // /* Dog          */  => format!("woof, woof! I am {}!", name),
        // /* Worm– or all silent animals?*/ => "-- (silence)".to_string(),
        _ => todo!()
    };
    
    /* Bonus task: Give Dogs named Lassie a different output */
    
    println!("{:?} says: {:?}", animal, noise);
}

fn main() {
    what_does_the_animal_say(
        &FarmAnimal::Dog {
            name: "Lassie".to_string()
    });
    what_does_the_animal_say(&FarmAnimal::Cow);
    what_does_the_animal_say(&FarmAnimal::Bull);
    what_does_the_animal_say(&FarmAnimal::Chicken{num_eggs: 3});
    what_does_the_animal_say(&FarmAnimal::Worm);

    /*
    Output should be:
    
    Dog { name: "Lassie" } says: "woof, woof! I am Lassie!"
    Cow says: "moo"
    Bull says: "moo"
    Chicken { num_eggs: 3 } says: "cluck, cluck!"
    Worm says: "-- (silence)"
    
    */
}

The current output is:

error[E0532]: expected tuple struct or tuple variant, found unit variant `FarmAnimal::Cow`
  --> src/main.rs:15:9
   |
15 |         FarmAnimal::Cow(_) => "moo".to_string(),
   |         ^^^^^^^^^^^^^^^ not a tuple struct or tuple variant

error: aborting due to previous error

For more information about this error, try `rustc --explain E0532`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Ideally the output should look like:

I'd like it to say "try to use a tuple unit variant like "FarmAnimal::Cow" instead".

For example, here's what happens if you use Tuple syntax instead of Struct syntax:

error[E0532]: expected tuple struct or tuple variant, found struct variant `FarmAnimal::Chicken`
  --> src/main.rs:16:9
   |
6  |     Chicken { num_eggs: usize },
   |     --------------------------- `FarmAnimal::Chicken` defined here
...
16 |         FarmAnimal::Chicken(x)  => "cluck, cluck!".to_string(),
   |         ^^^^^^^^^^^^^^^^^^^^^^ help: use struct pattern syntax instead: `FarmAnimal::Chicken { num_eggs }`

error: aborting due to 2 previous errors

So I'd like the new diagnostic to look something like:

error[Exxxx]: expected tuple struct or tuple variant, found unit variant `FarmAnimal::Cow`
  --> src/main.rs:16:9
   |
6  |     Cow,
   |     --- `FarmAnimal::Cow` defined here
...
16 |         FarmAnimal::Cow(_)  => "moo".to_string(),
   |         ^^^^^^^^^^^^^^^^^^ help: use unit pattern syntax instead: `FarmAnimal::Cow`

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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