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

unused_parens false positive on cast with match expression #88519

Closed
ehuss opened this issue Aug 30, 2021 · 3 comments · Fixed by #88547
Closed

unused_parens false positive on cast with match expression #88519

ehuss opened this issue Aug 30, 2021 · 3 comments · Fixed by #88547
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint 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`. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.

Comments

@ehuss
Copy link
Contributor

ehuss commented Aug 30, 2021

I tried this code:

pub fn foo() -> i32 {
    let x = 1;
    (match x {
        _ => x,
    } as i32)
}

This generates a warning about unused parentheses:

warning: unnecessary parentheses around block return value
 --> src/lib.rs:3:5
  |
3 | /     (match x {
4 | |         _ => x
5 | |     } as i32)
  | |_____________^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
3 ~     match x {
4 +         _ => x
5 +     } as i32
  |

However, applying the suggestion causes it to fail to compile:

error: expected expression, found `as`
 --> src/lib.rs:5:7
  |
5 |     } as i32
  |       ^^ expected expression
  |
help: parentheses are required to parse this as an expression
  |
3 ~     (match x {
4 |         _ => x,
5 ~     }) as i32
  |

Found in the 2021 crater run for:

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (5eacec9ec 2021-08-28)
binary: rustc
commit-hash: 5eacec9ec7e112a0de1011519a57c45586d58414
commit-date: 2021-08-28
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 13.0.0
@ehuss ehuss added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Aug 30, 2021
@ehuss
Copy link
Contributor Author

ehuss commented Aug 30, 2021

@chenyukang
Copy link
Member

chenyukang commented Aug 30, 2021

As the hint wrote, the parentheses should bed added in a different position:

pub fn foo() -> i32 {
let x = 1;
(match x {
_ => x,
})
as i32
}

@chenyukang
Copy link
Member

The second is:

pub fn foo_bar(val: i32) -> i32 {
    (if val == 1 { 0 } else { val }) as i32
}

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Sep 1, 2021
…necessary, r=davidtwco

fix(rustc_lint): better detect when parens are necessary

Fixes rust-lang#88519
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Sep 1, 2021
…necessary, r=davidtwco

fix(rustc_lint): better detect when parens are necessary

Fixes rust-lang#88519
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Sep 2, 2021
…necessary, r=davidtwco

fix(rustc_lint): better detect when parens are necessary

Fixes rust-lang#88519
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 4, 2021
…cessary, r=davidtwco

fix(rustc_lint): better detect when parens are necessary

Fixes rust-lang#88519
@bors bors closed this as completed in 59b245e Sep 4, 2021
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 A-lint 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`. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants