Skip to content

Commit d7e7510

Browse files
committed
Auto merge of rust-lang#113679 - chenyukang:yukang-fix-lint-113459, r=cjgillot
Match scrutinee need necessary parentheses for structs Fixes rust-lang#113459
2 parents ffaa32b + c44b35e commit d7e7510

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

compiler/rustc_lint/src/unused.rs

+18
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,24 @@ trait UnusedDelimLint {
666666
if !followed_by_block {
667667
return false;
668668
}
669+
670+
// Check if we need parens for `match &( Struct { feild: }) {}`.
671+
{
672+
let mut innermost = inner;
673+
loop {
674+
innermost = match &innermost.kind {
675+
ExprKind::AddrOf(_, _, expr) => expr,
676+
_ => {
677+
if parser::contains_exterior_struct_lit(&innermost) {
678+
return true;
679+
} else {
680+
break;
681+
}
682+
}
683+
}
684+
}
685+
}
686+
669687
let mut innermost = inner;
670688
loop {
671689
innermost = match &innermost.kind {
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![allow(dead_code)]
2+
#![deny(unused_parens)]
3+
4+
enum State {
5+
Waiting { start_at: u64 }
6+
}
7+
struct Foo {}
8+
9+
fn main() {
10+
let e = &mut State::Waiting { start_at: 0u64 };
11+
match (&mut State::Waiting { start_at: 0u64 }) {
12+
_ => {}
13+
}
14+
15+
match (e) {
16+
//~^ ERROR unnecessary parentheses around `match` scrutinee expression
17+
_ => {}
18+
}
19+
20+
match &(State::Waiting { start_at: 0u64 }) {
21+
_ => {}
22+
}
23+
24+
match (State::Waiting { start_at: 0u64 }) {
25+
_ => {}
26+
}
27+
28+
match (&&Foo {}) {
29+
_ => {}
30+
}
31+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: unnecessary parentheses around `match` scrutinee expression
2+
--> $DIR/lint-struct-necessary.rs:15:11
3+
|
4+
LL | match (e) {
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-struct-necessary.rs:2:9
9+
|
10+
LL | #![deny(unused_parens)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these parentheses
13+
|
14+
LL - match (e) {
15+
LL + match e {
16+
|
17+
18+
error: aborting due to previous error
19+

0 commit comments

Comments
 (0)