Skip to content

unused_parens false positive on cast with match expression #88519

Closed
@ehuss

Description

@ehuss

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions