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

Bad help suggestion in unused variable warning #50804

Closed
wesleywiser opened this issue May 16, 2018 · 3 comments · Fixed by #50854
Closed

Bad help suggestion in unused variable warning #50804

wesleywiser opened this issue May 16, 2018 · 3 comments · Fixed by #50854
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@wesleywiser
Copy link
Member

I have the following code:

struct Test {
    a: usize,
    b: usize
}

fn test(x: Test) {
    let Test {
        a,
    } = x;
    
    println!("{:?}", a);
}

Since I don't need the value of b, I didn't add a binding for it in the struct destructuring. This generates an error:

error[E0027]: pattern does not mention field `b`
 --> src/main.rs:7:9
  |
7 |       let Test {
  |  _________^
8 | |         a,
9 | |     } = x;
  | |_____^ missing field `b`

error: aborting due to previous error

This makes sense to me so I'll go ahead and add b:

    let Test {
        a,
        b
    } = x;

but then this complains that b isn't used:

warning: unused variable: `b`
 --> src/main.rs:9:9
  |
9 |         b
  |         ^ help: consider using `_b` instead
  |
  = note: #[warn(unused_variables)] on by default

That also makes sense and I've used the _ prefix before to ignore values so I'll try that:

    let Test {
        a,
        _b
    } = x;

but now I get errors:

error[E0026]: struct `Test` does not have a field named `_b`
 --> src/main.rs:9:9
  |
9 |         _b
  |         ^^ struct `Test` does not have this field

error[E0027]: pattern does not mention field `b`
  --> src/main.rs:7:9
   |
7  |       let Test {
   |  _________^
8  | |         a,
9  | |         _b
10 | |     } = x;
   | |_____^ missing field `b`

error: aborting due to 2 previous errors

TLDR: We shouldn't offer the _b suggestion if it isn't valid.

@varkor
Copy link
Member

varkor commented May 16, 2018

This should have been addressed by #47922. I imagine altering it to fix this case shouldn't be too difficult: it's probably that only match arm patterns (including desugared let) are considered for this lint, rather than destructuring assignment.

@varkor varkor added the A-diagnostics Area: Messages for errors, warnings, and lints label May 16, 2018
@zackmdavis
Copy link
Member

(PR forthcoming)

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.
@wesleywiser
Copy link
Member Author

@zackmdavis Thanks for fixing that so quick! Awesome branch name btw 😆

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

Successfully merging a pull request may close this issue.

3 participants