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

Incorrect unused_variables diagnostic for or-ed patterns #67691

Closed
sapir opened this issue Dec 29, 2019 · 3 comments
Closed

Incorrect unused_variables diagnostic for or-ed patterns #67691

sapir opened this issue Dec 29, 2019 · 3 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-or_patterns `#![feature(or_patterns)]` regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sapir
Copy link
Contributor

sapir commented Dec 29, 2019

With rustc 1.42.0-nightly (0de96d3 2019-12-19):

For the following code:

pub enum MyEnum {
    A { i: i32, j: i32 },
    B { i: i32, j: i32 },
}

pub fn foo(x: MyEnum) {
    use MyEnum::*;

    match x {
        A { i, j } | B { i, j } => {
            println!("{}", i);
        }
    }
}

I get the following diagnostic:

) rustc +nightly test.rs -o test --crate-type lib
warning: unused variable: `j`
  --> test.rs:10:16
   |
10 |         A { i, j } | B { i, j } => {
   |                ^            ^
   |
   = note: `#[warn(unused_variables)]` on by default
help: try ignoring the field
   |
10 |         A { i, j: _j: _ } | B { i, j } => {
   |                ^^^^^^^

Instead of:

help: try ignoring the field
   |
10 |         A { i, j: _ } | B { i, j: _ } => {
   |                ^^^^            ^^^^
@Centril Centril added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. F-or_patterns `#![feature(or_patterns)]` regression-from-stable-to-stable Performance or correctness regression from one stable version to another. A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. labels Dec 29, 2019
@Centril
Copy link
Contributor

Centril commented Dec 29, 2019

Regression introduced in 1.36.0.

@sapir
Copy link
Contributor Author

sapir commented Dec 31, 2019

I found the breaking commit with cargo-bisect-rustc and git bisect. #59993

Based on this, I found that if the or pattern uses ref then the diagnostic becomes more confused.

I can have a try at fixing this later, if that's ok.

bors pushed a commit to rust-lang-ci/rust that referenced this issue Apr 12, 2020
bors pushed a commit to rust-lang-ci/rust that referenced this issue Apr 12, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 12, 2020
…ing, r=matthewjasper

Fix warning for unused variables in or pattern (issue rust-lang#67691)

Is this a good way to fix it?

Also, the tests fail, the "fixed" code output says `{ i, j }` instead of `{ i, j: _ }`, how can I fix that?
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 12, 2020
Rollup of 4 pull requests

Successful merges:

 - rust-lang#67766 (Fix warning for unused variables in or pattern (issue rust-lang#67691))
 - rust-lang#71013 (Pass the `PlaceElem::Index` local to `visit_local`)
 - rust-lang#71064 (fix issue 69130)
 - rust-lang#71069 (Remove some usage of `DUMMY_HIR_ID`)

Failed merges:

r? @ghost
@Centril
Copy link
Contributor

Centril commented Apr 13, 2020

Was fixed in #67766.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-or_patterns `#![feature(or_patterns)]` regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants