Skip to content

Suggest a missing field when the rest of the field is found #87938

Closed
@hkmatsumoto

Description

@hkmatsumoto
Member

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2c9af7b21295b19d151a109261981cd2

enum Foo {
    Bar { alpha: u8, bravo: u8, charlie: u8 },
}

fn foo(foo: Foo) {
    match foo {
        Foo::Bar {
            alpha,
            beta, // `bravo` miswritten as `beta` here.
            charlie,
        } => todo!(),
    }
}

The current output is:

error[E0026]: variant `Foo::Bar` does not have a field named `beta`
 --> src/lib.rs:9:13
  |
9 |             beta, // `bravo` miswritten as `beta` here.
  |             ^^^^ variant `Foo::Bar` does not have this field

error[E0027]: pattern does not mention field `bravo`
  --> src/lib.rs:7:9
   |
7  | /         Foo::Bar {
8  | |             alpha,
9  | |             beta, // `bravo` miswritten as `beta` here.
10 | |             charlie,
11 | |         } => todo!(),
   | |_________^ missing field `bravo`
   |
help: include the missing field in the pattern
   |
10 |             charlie, bravo } => todo!(),
   |                    ^^^^^^^^^
help: if you don't care about this missing field, you can explicitly ignore it
   |
10 |             charlie, .. } => todo!(),
   |                    ^^^^^^

error: aborting due to 2 previous errors

Ideally the output should look like:

error[E0026]: variant `Foo::Bar` does not have a field named `beta`
 --> src/lib.rs:9:13
  |
9 |             beta, // `bravo` miswritten as `beta` here.
  |             ^^^^ help: maybe replacing this for a missing field: `bravo`

We already provide a suggestion for similar names, for example, brav0 is suggested to replace it for bravo.
Analogous to that, when the rest of the field (alpha & charlie) is mentioned, suggesting to replace the miswritten field (beta) for the missing field (bravo) can be helpful for users.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Aug 11, 2021
added 2 commits that reference this issue on Sep 19, 2021

Rollup merge of rust-lang#87960 - hkmatsumoto:suggest-inexisting-fiel…

2f210be

Rollup merge of rust-lang#87960 - hkmatsumoto:suggest-inexisting-fiel…

ebd31f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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

      Participants

      @hkmatsumoto

      Issue actions

        Suggest a missing field when the rest of the field is found · Issue #87938 · rust-lang/rust