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

Poor help error message for unused variable on struct enum match #50303

Closed
dandc87 opened this issue Apr 28, 2018 · 1 comment
Closed

Poor help error message for unused variable on struct enum match #50303

dandc87 opened this issue Apr 28, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@dandc87
Copy link

dandc87 commented Apr 28, 2018

Suppose I've written code like:

enum Foo {
    Bar { bar: u32 },
    Baz,
}

fn is_bar(foo: &Foo) -> bool {
    match foo {
        &Foo::Bar { bar } => true,
        &Foo::Baz => false,
    }
}

The bar variable is unused, and the error message I get looks like:

warning: unused variable: `bar`
  --> src/main.rs:37:21
   |
37 |         &Foo::Bar { bar } => true,
   |                     ^^^ help: consider using `_bar` instead
   |
   = note: #[warn(unused_variables)] on by default

The help is incorrect; _bar is invalid. It should suggest replacing bar with bar: _

@zackmdavis
Copy link
Member

This bad suggestion was fixed in most cases by #47922, but this case would seem to have evaded it. (Notice that on Nightly you can omit the ampersands from the match guards (due to the stabilization of the match-default-bindings ergonomic) and receive the correct suggestion!)

@sfackler sfackler added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 28, 2018
kennytm added a commit to kennytm/rust that referenced this issue Apr 30, 2018
…=estebank

Display correct unused field suggestion for nested struct patterns

Extends rust-lang#47922 by checking more sophisticated patterns (e.g. references, slices, etc.).
Before:
```
warning: unused variable: `bar`
  --> src/main.rs:37:21
   |
37 |         &Foo::Bar { bar } => true,
   |                     ^^^ help: consider using `_bar` instead
   |
   = note: #[warn(unused_variables)] on by default
```
After:
```
warning: unused variable: `bar`
  --> src/main.rs:37:21
   |
37 |         &Foo::Bar { bar } => true,
   |                     ^^^ help: try ignoring the field: `bar: _`
   |
   = note: #[warn(unused_variables)] on by default
```

Fixes rust-lang#50303.

r? @estebank
zackmdavis added a commit to zackmdavis/rust that referenced this issue May 18, 2018
In e4b1a79 (rust-lang#47922), we corrected erroneous suggestions for unused
shorthand field pattern bindings, suggesting `field: _` where the
previous suggestion of `_field` wouldn't even have compiled
(rust-lang#47390). Soon, it was revealed that this was insufficient (rust-lang#50303), and
the fix was extended to references, slices, &c. (rust-lang#50327) But even this
proved inadequate, as the erroneous suggestions were still being issued
for patterns in local (`let`) bindings (rust-lang#50804). Here, we yank the
shorthand-detection and variable/node registration code into a new
common function that can be called while visiting both match arms and
`let` bindings.

Resolves rust-lang#50804.
kennytm added a commit to kennytm/rust that referenced this issue May 19, 2018
…ed_field_pattern_3_straight_to_video, r=estebank

in which the unused shorthand field pattern debacle/saga continues

In e4b1a79 (rust-lang#47922), we corrected erroneous suggestions for unused
shorthand field pattern bindings, suggesting `field: _` where the
previous suggestion of `_field` wouldn't even have compiled
(rust-lang#47390). Soon, it was revealed that this was insufficient (rust-lang#50303), and
the fix was extended to references, slices, &c. (rust-lang#50327) But even this
proved inadequate, as the erroneous suggestions were still being issued
for patterns in local (`let`) bindings (rust-lang#50804). Here, we yank the
shorthand-detection and variable/node registration code into a new
common function that can be called while visiting both match arms and
`let` bindings.

Resolves rust-lang#50804.

r? @estebank
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
Projects
None yet
Development

No branches or pull requests

3 participants