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

Suggestion for unused variables can sometimes be incorrect #56685

Closed
alexcrichton opened this issue Dec 10, 2018 · 0 comments · Fixed by #57899
Closed

Suggestion for unused variables can sometimes be incorrect #56685

alexcrichton opened this issue Dec 10, 2018 · 0 comments · Fixed by #57899
Assignees
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`

Comments

@alexcrichton
Copy link
Member

First reported upstream this code:

fn main() {
    enum E {
        A(i32,),
        B(i32,),
    }
    match E::A(1) {
        E::A(x) | E::B(x) => {}
    }
}

when compiled yields a suggestion of

warning: unused variable: `x`
 --> src/main.rs:7:14
  |
7 |         E::A(x) | E::B(x) => {}
  |              ^ help: consider using `_x` instead
  |
  = note: #[warn(unused_variables)] on by default

warning: variant is never constructed: `B`
 --> src/main.rs:4:9
  |
4 |         B(i32),
  |         ^^^^^^
  |
  = note: #[warn(dead_code)] on by default

but the suggestion is not correct! Both instances of the x binding need to be renamed in order for the code to be automatically fixed.

@alexcrichton alexcrichton added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Dec 10, 2018
@alexcrichton alexcrichton added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Jan 7, 2019
@davidtwco davidtwco self-assigned this Jan 25, 2019
Centril added a commit to Centril/rust that referenced this issue Jan 27, 2019
Unused variable suggestions apply on all patterns.

Fixes rust-lang#56685.

This PR extends existing suggestions to prefix unused variable bindings in match arms with an underscore so that it applies to all patterns in a match arm.

r? @estebank
cc @alexcrichton (since you filed the issue)
Centril added a commit to Centril/rust that referenced this issue Jan 27, 2019
Unused variable suggestions apply on all patterns.

Fixes rust-lang#56685.

This PR extends existing suggestions to prefix unused variable bindings in match arms with an underscore so that it applies to all patterns in a match arm.

r? @estebank
cc @alexcrichton (since you filed the issue)
@bors bors closed this as completed in a21bd75 Jan 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants