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

Match scrutinee need necessary parentheses for structs #113679

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,24 @@ trait UnusedDelimLint {
if !followed_by_block {
return false;
}

// Check if we need parens for `match &( Struct { feild: }) {}`.
{
let mut innermost = inner;
loop {
innermost = match &innermost.kind {
ExprKind::AddrOf(_, _, expr) => expr,
_ => {
if parser::contains_exterior_struct_lit(&innermost) {
return true;
} else {
break;
}
}
}
}
}

let mut innermost = inner;
loop {
innermost = match &innermost.kind {
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/lint/lint-struct-necessary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#![allow(dead_code)]
#![deny(unused_parens)]

enum State {
Waiting { start_at: u64 }
}
struct Foo {}

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

match (e) {
//~^ ERROR unnecessary parentheses around `match` scrutinee expression
_ => {}
}

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

match (State::Waiting { start_at: 0u64 }) {
_ => {}
}

match (&&Foo {}) {
_ => {}
}
}
19 changes: 19 additions & 0 deletions tests/ui/lint/lint-struct-necessary.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error: unnecessary parentheses around `match` scrutinee expression
--> $DIR/lint-struct-necessary.rs:15:11
|
LL | match (e) {
| ^ ^
|
note: the lint level is defined here
--> $DIR/lint-struct-necessary.rs:2:9
|
LL | #![deny(unused_parens)]
| ^^^^^^^^^^^^^
help: remove these parentheses
|
LL - match (e) {
LL + match e {
|

error: aborting due to previous error

Loading