Skip to content

Commit e9d2fef

Browse files
committed
stop check paren if has different ctx
1 parent efcbb94 commit e9d2fef

4 files changed

+139
-0
lines changed

compiler/rustc_lint/src/unused.rs

+16
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,22 @@ trait UnusedDelimLint {
942942
match s.kind {
943943
StmtKind::Let(ref local) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => {
944944
if let Some((init, els)) = local.kind.init_else_opt() {
945+
if els.is_some()
946+
&& let ExprKind::Paren(paren) = &init.kind
947+
&& !init.span.eq_ctxt(paren.span)
948+
{
949+
// This branch prevents cases where parentheses wrap an expression
950+
// resulting from macro expansion, such as:
951+
// ```
952+
// macro_rules! x {
953+
// () => { None::<i32> };
954+
// }
955+
// let Some(_) = (x!{}) else { return };
956+
// // -> let Some(_) = (None::<i32>) else { return };
957+
// // ~ ~ No Lint
958+
// ```
959+
return;
960+
}
945961
let ctx = match els {
946962
None => UnusedDelimsCtx::AssignedValue,
947963
Some(_) => UnusedDelimsCtx::AssignedValueLetElse,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ run-rustfix
2+
3+
#![deny(unused_parens)]
4+
5+
fn main() {
6+
macro_rules! x {
7+
() => { None::<i32> };
8+
}
9+
10+
let Some(_) = (x!{}) else { return }; // no error
11+
let Some(_) = (x!{}) else { return };
12+
//~^ ERROR: unnecessary parentheses around assigned value
13+
14+
let Some(_) = (x!{}) else { return };
15+
//~^ ERROR: unnecessary parentheses around pattern
16+
17+
let _ = x!{};
18+
let _ = x!{};
19+
//~^ ERROR: unnecessary parentheses around assigned value
20+
21+
if let Some(_) = x!{} {};
22+
if let Some(_) = x!{} {};
23+
//~^ ERROR: unnecessary parentheses around `let` scrutinee expression
24+
25+
while let Some(_) = x!{} {};
26+
while let Some(_) = x!{} {};
27+
//~^ ERROR: unnecessary parentheses around `let` scrutinee expression
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ run-rustfix
2+
3+
#![deny(unused_parens)]
4+
5+
fn main() {
6+
macro_rules! x {
7+
() => { None::<i32> };
8+
}
9+
10+
let Some(_) = (x!{}) else { return }; // no error
11+
let Some(_) = ((x!{})) else { return };
12+
//~^ ERROR: unnecessary parentheses around assigned value
13+
14+
let Some((_)) = (x!{}) else { return };
15+
//~^ ERROR: unnecessary parentheses around pattern
16+
17+
let _ = x!{};
18+
let _ = (x!{});
19+
//~^ ERROR: unnecessary parentheses around assigned value
20+
21+
if let Some(_) = x!{} {};
22+
if let Some(_) = (x!{}) {};
23+
//~^ ERROR: unnecessary parentheses around `let` scrutinee expression
24+
25+
while let Some(_) = x!{} {};
26+
while let Some(_) = (x!{}) {};
27+
//~^ ERROR: unnecessary parentheses around `let` scrutinee expression
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
error: unnecessary parentheses around assigned value
2+
--> $DIR/unused-parens-for-macro-call-with-brace.rs:11:19
3+
|
4+
LL | let Some(_) = ((x!{})) else { return };
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-parens-for-macro-call-with-brace.rs:3:9
9+
|
10+
LL | #![deny(unused_parens)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these parentheses
13+
|
14+
LL - let Some(_) = ((x!{})) else { return };
15+
LL + let Some(_) = (x!{}) else { return };
16+
|
17+
18+
error: unnecessary parentheses around pattern
19+
--> $DIR/unused-parens-for-macro-call-with-brace.rs:14:14
20+
|
21+
LL | let Some((_)) = (x!{}) else { return };
22+
| ^ ^
23+
|
24+
help: remove these parentheses
25+
|
26+
LL - let Some((_)) = (x!{}) else { return };
27+
LL + let Some(_) = (x!{}) else { return };
28+
|
29+
30+
error: unnecessary parentheses around assigned value
31+
--> $DIR/unused-parens-for-macro-call-with-brace.rs:18:13
32+
|
33+
LL | let _ = (x!{});
34+
| ^ ^
35+
|
36+
help: remove these parentheses
37+
|
38+
LL - let _ = (x!{});
39+
LL + let _ = x!{};
40+
|
41+
42+
error: unnecessary parentheses around `let` scrutinee expression
43+
--> $DIR/unused-parens-for-macro-call-with-brace.rs:22:22
44+
|
45+
LL | if let Some(_) = (x!{}) {};
46+
| ^ ^
47+
|
48+
help: remove these parentheses
49+
|
50+
LL - if let Some(_) = (x!{}) {};
51+
LL + if let Some(_) = x!{} {};
52+
|
53+
54+
error: unnecessary parentheses around `let` scrutinee expression
55+
--> $DIR/unused-parens-for-macro-call-with-brace.rs:26:25
56+
|
57+
LL | while let Some(_) = (x!{}) {};
58+
| ^ ^
59+
|
60+
help: remove these parentheses
61+
|
62+
LL - while let Some(_) = (x!{}) {};
63+
LL + while let Some(_) = x!{} {};
64+
|
65+
66+
error: aborting due to 5 previous errors
67+

0 commit comments

Comments
 (0)