Skip to content

Commit 8df1f41

Browse files
author
Lukas Markeffsky
committed
fix false positives for unused_parens around unary and binary operations
1 parent e14b81f commit 8df1f41

File tree

4 files changed

+64
-36
lines changed

4 files changed

+64
-36
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -569,36 +569,48 @@ trait UnusedDelimLint {
569569
}
570570
}
571571

572-
// Prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`
573-
let lhs_needs_parens = {
572+
// Check if LHS needs parens to prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`.
573+
{
574574
let mut innermost = inner;
575575
loop {
576576
innermost = match &innermost.kind {
577-
ExprKind::Binary(_, lhs, _rhs) => lhs,
577+
ExprKind::Binary(_op, lhs, _rhs) => lhs,
578578
ExprKind::Call(fn_, _params) => fn_,
579579
ExprKind::Cast(expr, _ty) => expr,
580580
ExprKind::Type(expr, _ty) => expr,
581581
ExprKind::Index(base, _subscript) => base,
582-
_ => break false,
582+
_ => break,
583583
};
584584
if !classify::expr_requires_semi_to_be_stmt(innermost) {
585-
break true;
585+
return true;
586586
}
587587
}
588-
};
588+
}
589589

590-
lhs_needs_parens
591-
|| (followed_by_block
592-
&& match &inner.kind {
593-
ExprKind::Ret(_)
594-
| ExprKind::Break(..)
595-
| ExprKind::Yield(..)
596-
| ExprKind::Yeet(..) => true,
597-
ExprKind::Range(_lhs, Some(rhs), _limits) => {
598-
matches!(rhs.kind, ExprKind::Block(..))
599-
}
600-
_ => parser::contains_exterior_struct_lit(&inner),
601-
})
590+
// Check if RHS needs parens to prevent false-positives in cases like `if (() == return) {}`.
591+
if !followed_by_block {
592+
return false;
593+
}
594+
let mut innermost = inner;
595+
loop {
596+
innermost = match &innermost.kind {
597+
ExprKind::Unary(_op, expr) => expr,
598+
ExprKind::Binary(_op, _lhs, rhs) => rhs,
599+
ExprKind::AssignOp(_op, _lhs, rhs) => rhs,
600+
ExprKind::Assign(_lhs, rhs, _span) => rhs,
601+
602+
ExprKind::Ret(_)
603+
| ExprKind::Break(..)
604+
| ExprKind::Yield(..)
605+
| ExprKind::Yeet(..) => return true,
606+
607+
ExprKind::Range(_lhs, Some(rhs), _limits) => {
608+
return matches!(rhs.kind, ExprKind::Block(..));
609+
}
610+
611+
_ => return parser::contains_exterior_struct_lit(&inner),
612+
}
613+
}
602614
}
603615

604616
fn emit_unused_delims_expr(

tests/ui/lint/unused/issue-54538-unused-parens-lint.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ fn _no_lint_yeet() -> Result<(), ()> {
3232
Ok(())
3333
}
3434

35+
fn _no_lint_ops() {
36+
#![allow(unreachable_code, irrefutable_let_patterns)]
37+
if ((..{}) == ..{}) {}
38+
if (!return) {}
39+
loop { match (() = () = () = break {}) {} }
40+
while let () = (*&mut false |= true && return) {}
41+
}
42+
3543
// Don't lint in these cases (#64106).
3644
fn or_patterns_no_lint() {
3745
match Box::new(0) {

tests/ui/lint/unused/issue-54538-unused-parens-lint.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ fn _no_lint_yeet() -> Result<(), ()> {
3232
Ok(())
3333
}
3434

35+
fn _no_lint_ops() {
36+
#![allow(unreachable_code, irrefutable_let_patterns)]
37+
if ((..{}) == ..{}) {}
38+
if (!return) {}
39+
loop { match (() = () = () = break {}) {} }
40+
while let () = (*&mut false |= true && return) {}
41+
}
42+
3543
// Don't lint in these cases (#64106).
3644
fn or_patterns_no_lint() {
3745
match Box::new(0) {

tests/ui/lint/unused/issue-54538-unused-parens-lint.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ LL + let _ = |a: u8| 0;
7676
|
7777

7878
error: unnecessary parentheses around pattern
79-
--> $DIR/issue-54538-unused-parens-lint.rs:56:12
79+
--> $DIR/issue-54538-unused-parens-lint.rs:64:12
8080
|
8181
LL | if let (0 | 1) = 0 {}
8282
| ^ ^
@@ -88,7 +88,7 @@ LL + if let 0 | 1 = 0 {}
8888
|
8989

9090
error: unnecessary parentheses around pattern
91-
--> $DIR/issue-54538-unused-parens-lint.rs:57:13
91+
--> $DIR/issue-54538-unused-parens-lint.rs:65:13
9292
|
9393
LL | if let ((0 | 1),) = (0,) {}
9494
| ^ ^
@@ -100,7 +100,7 @@ LL + if let (0 | 1,) = (0,) {}
100100
|
101101

102102
error: unnecessary parentheses around pattern
103-
--> $DIR/issue-54538-unused-parens-lint.rs:58:13
103+
--> $DIR/issue-54538-unused-parens-lint.rs:66:13
104104
|
105105
LL | if let [(0 | 1)] = [0] {}
106106
| ^ ^
@@ -112,7 +112,7 @@ LL + if let [0 | 1] = [0] {}
112112
|
113113

114114
error: unnecessary parentheses around pattern
115-
--> $DIR/issue-54538-unused-parens-lint.rs:59:16
115+
--> $DIR/issue-54538-unused-parens-lint.rs:67:16
116116
|
117117
LL | if let 0 | (1 | 2) = 0 {}
118118
| ^ ^
@@ -124,7 +124,7 @@ LL + if let 0 | 1 | 2 = 0 {}
124124
|
125125

126126
error: unnecessary parentheses around pattern
127-
--> $DIR/issue-54538-unused-parens-lint.rs:61:15
127+
--> $DIR/issue-54538-unused-parens-lint.rs:69:15
128128
|
129129
LL | if let TS((0 | 1)) = TS(0) {}
130130
| ^ ^
@@ -136,7 +136,7 @@ LL + if let TS(0 | 1) = TS(0) {}
136136
|
137137

138138
error: unnecessary parentheses around pattern
139-
--> $DIR/issue-54538-unused-parens-lint.rs:63:20
139+
--> $DIR/issue-54538-unused-parens-lint.rs:71:20
140140
|
141141
LL | if let NS { f: (0 | 1) } = (NS { f: 0 }) {}
142142
| ^ ^
@@ -148,7 +148,7 @@ LL + if let NS { f: 0 | 1 } = (NS { f: 0 }) {}
148148
|
149149

150150
error: unnecessary parentheses around pattern
151-
--> $DIR/issue-54538-unused-parens-lint.rs:73:9
151+
--> $DIR/issue-54538-unused-parens-lint.rs:81:9
152152
|
153153
LL | (_) => {}
154154
| ^ ^
@@ -160,7 +160,7 @@ LL + _ => {}
160160
|
161161

162162
error: unnecessary parentheses around pattern
163-
--> $DIR/issue-54538-unused-parens-lint.rs:74:9
163+
--> $DIR/issue-54538-unused-parens-lint.rs:82:9
164164
|
165165
LL | (y) => {}
166166
| ^ ^
@@ -172,7 +172,7 @@ LL + y => {}
172172
|
173173

174174
error: unnecessary parentheses around pattern
175-
--> $DIR/issue-54538-unused-parens-lint.rs:75:9
175+
--> $DIR/issue-54538-unused-parens-lint.rs:83:9
176176
|
177177
LL | (ref r) => {}
178178
| ^ ^
@@ -184,7 +184,7 @@ LL + ref r => {}
184184
|
185185

186186
error: unnecessary parentheses around pattern
187-
--> $DIR/issue-54538-unused-parens-lint.rs:76:9
187+
--> $DIR/issue-54538-unused-parens-lint.rs:84:9
188188
|
189189
LL | (e @ 1...2) => {}
190190
| ^ ^
@@ -196,7 +196,7 @@ LL + e @ 1...2 => {}
196196
|
197197

198198
error: unnecessary parentheses around pattern
199-
--> $DIR/issue-54538-unused-parens-lint.rs:82:9
199+
--> $DIR/issue-54538-unused-parens-lint.rs:90:9
200200
|
201201
LL | (e @ &(1...2)) => {}
202202
| ^ ^
@@ -208,7 +208,7 @@ LL + e @ &(1...2) => {}
208208
|
209209

210210
error: unnecessary parentheses around pattern
211-
--> $DIR/issue-54538-unused-parens-lint.rs:83:10
211+
--> $DIR/issue-54538-unused-parens-lint.rs:91:10
212212
|
213213
LL | &(_) => {}
214214
| ^ ^
@@ -220,7 +220,7 @@ LL + &_ => {}
220220
|
221221

222222
error: unnecessary parentheses around pattern
223-
--> $DIR/issue-54538-unused-parens-lint.rs:94:9
223+
--> $DIR/issue-54538-unused-parens-lint.rs:102:9
224224
|
225225
LL | (_) => {}
226226
| ^ ^
@@ -232,7 +232,7 @@ LL + _ => {}
232232
|
233233

234234
error: unnecessary parentheses around pattern
235-
--> $DIR/issue-54538-unused-parens-lint.rs:95:9
235+
--> $DIR/issue-54538-unused-parens-lint.rs:103:9
236236
|
237237
LL | (y) => {}
238238
| ^ ^
@@ -244,7 +244,7 @@ LL + y => {}
244244
|
245245

246246
error: unnecessary parentheses around pattern
247-
--> $DIR/issue-54538-unused-parens-lint.rs:96:9
247+
--> $DIR/issue-54538-unused-parens-lint.rs:104:9
248248
|
249249
LL | (ref r) => {}
250250
| ^ ^
@@ -256,7 +256,7 @@ LL + ref r => {}
256256
|
257257

258258
error: unnecessary parentheses around pattern
259-
--> $DIR/issue-54538-unused-parens-lint.rs:97:9
259+
--> $DIR/issue-54538-unused-parens-lint.rs:105:9
260260
|
261261
LL | (e @ 1..=2) => {}
262262
| ^ ^
@@ -268,7 +268,7 @@ LL + e @ 1..=2 => {}
268268
|
269269

270270
error: unnecessary parentheses around pattern
271-
--> $DIR/issue-54538-unused-parens-lint.rs:103:9
271+
--> $DIR/issue-54538-unused-parens-lint.rs:111:9
272272
|
273273
LL | (e @ &(1..=2)) => {}
274274
| ^ ^
@@ -280,7 +280,7 @@ LL + e @ &(1..=2) => {}
280280
|
281281

282282
error: unnecessary parentheses around pattern
283-
--> $DIR/issue-54538-unused-parens-lint.rs:104:10
283+
--> $DIR/issue-54538-unused-parens-lint.rs:112:10
284284
|
285285
LL | &(_) => {}
286286
| ^ ^

0 commit comments

Comments
 (0)