match scrutinee need necessary parentheses for structs

This commit is contained in:
yukang 2023-07-14 08:20:29 +08:00
parent 3071e0aef6
commit c44b35e1c3
3 changed files with 68 additions and 0 deletions

View file

@ -666,6 +666,24 @@ fn is_expr_delims_necessary(
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 {

View file

@ -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 {}) {
_ => {}
}
}

View file

@ -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