Skip to content

unused_parens reported on complex enum struct-variant literal in match scrutinee expression is actually necessary #113459

Closed
@jieyouxu

Description

@jieyouxu

Code

#![allow(dead_code)]

enum State {
    Waiting { start_at: u64 }
}

fn main() {
    match (&mut State::Waiting { start_at: 0u64 }) {
        _ => {}
    }
}

Current output

warning: unnecessary parentheses around `match` scrutinee expression
 --> src/main.rs:8:11
  |
8 |     match (&mut State::Waiting { start_at: 0u64 }) {
  |           ^                                      ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
8 -     match (&mut State::Waiting { start_at: 0u64 }) {
8 +     match &mut State::Waiting { start_at: 0u64 } {
  |

Desired output

No `unused_parens` warning.

Rationale and extra context

The unused_parens lint should not fire in this case because the parentheses are actually necessary. Otherwise, following the help to "remove these parentheses" will cause a compilation error.

error: struct literals are not allowed here
 --> src/main.rs:8:16
  |
8 |     match &mut State::Waiting { start_at: 0u64 } {
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
help: surround the struct literal with parentheses
  |
8 |     match &mut (State::Waiting { start_at: 0u64 }) {
  |                +                                 +

Although, following help from the error after applying the suggestion from the unused_parens lint does lead to valid code.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions