Skip to content

"Unused variable" warning gives incorrect suggestion when binding fields of struct-like variants #47390

Closed
@elidupree

Description

@elidupree

Here's a short example:

enum Enum {
  Variant {field: u32},
  OtherVariant,
}

fn main() {
  let whatever = Enum::Variant{field:0};
  if let Enum::Variant {field} = whatever {
    // field left unused
  }
}

This produces the warning:

warning: unused variable: `field`
 --> src/main.rs:8:25
  |
8 |   if let Enum::Variant {field} = whatever {
  |                         ^^^^^
  |
  = note: #[warn(unused_variables)] on by default
  = note: to avoid this warning, consider using `_field` instead

But changing it to _field is an error because that's not the name of the field. The warning should probably suggest to use Enum::Variant {..} instead.

This also occurs in explicit match statements and irrefutable let patterns – probably anywhere you can bind a field of a struct-like variant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions