Skip to content

Commit 06d12f6

Browse files
authored
Rollup merge of #110257 - lukas-code:why-would-anyone-write-code-like-that-anyway, r=oli-obk
fix false positives for `unused_parens` around unary and binary operations fix #110251
2 parents 91fe117 + 0d0949d commit 06d12f6

File tree

4 files changed

+109
-41
lines changed

4 files changed

+109
-41
lines changed

Diff for: compiler/rustc_lint/src/unused.rs

+32-18
Original file line numberDiff line numberDiff line change
@@ -569,36 +569,50 @@ 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(_) | ExprKind::Yield(..) | ExprKind::Yeet(..) => return true,
603+
604+
ExprKind::Break(_label, None) => return false,
605+
ExprKind::Break(_label, Some(break_expr)) => {
606+
return matches!(break_expr.kind, ExprKind::Block(..));
607+
}
608+
609+
ExprKind::Range(_lhs, Some(rhs), _limits) => {
610+
return matches!(rhs.kind, ExprKind::Block(..));
611+
}
612+
613+
_ => return parser::contains_exterior_struct_lit(&inner),
614+
}
615+
}
602616
}
603617

604618
fn emit_unused_delims_expr(

Diff for: tests/ui/lint/unused/issue-54538-unused-parens-lint.fixed

+17-2
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,32 @@ fn lint_on_top_level() {
2121
let _ = |a: u8| 0; //~ ERROR unnecessary parentheses around pattern
2222
}
2323

24-
fn _no_lint_attr() {
24+
fn no_lint_attr() {
2525
let _x = #[allow(dead_code)] (1 + 2);
2626
}
2727

28-
fn _no_lint_yeet() -> Result<(), ()> {
28+
fn no_lint_yeet() -> Result<(), ()> {
2929
#[allow(unreachable_code)]
3030
if (do yeet) {}
3131

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+
43+
fn lint_break_if_not_followed_by_block() {
44+
#![allow(unreachable_code)]
45+
loop { if break {} } //~ ERROR unnecessary parentheses
46+
loop { if break ({ println!("hello") }) {} } //~ ERROR unnecessary parentheses
47+
loop { if (break { println!("hello") }) {} }
48+
}
49+
3550
// Don't lint in these cases (#64106).
3651
fn or_patterns_no_lint() {
3752
match Box::new(0) {

Diff for: tests/ui/lint/unused/issue-54538-unused-parens-lint.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,32 @@ fn lint_on_top_level() {
2121
let _ = |(a): u8| 0; //~ ERROR unnecessary parentheses around pattern
2222
}
2323

24-
fn _no_lint_attr() {
24+
fn no_lint_attr() {
2525
let _x = #[allow(dead_code)] (1 + 2);
2626
}
2727

28-
fn _no_lint_yeet() -> Result<(), ()> {
28+
fn no_lint_yeet() -> Result<(), ()> {
2929
#[allow(unreachable_code)]
3030
if (do yeet) {}
3131

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+
43+
fn lint_break_if_not_followed_by_block() {
44+
#![allow(unreachable_code)]
45+
loop { if (break) {} } //~ ERROR unnecessary parentheses
46+
loop { if (break ({ println!("hello") })) {} } //~ ERROR unnecessary parentheses
47+
loop { if (break { println!("hello") }) {} }
48+
}
49+
3550
// Don't lint in these cases (#64106).
3651
fn or_patterns_no_lint() {
3752
match Box::new(0) {

Diff for: tests/ui/lint/unused/issue-54538-unused-parens-lint.stderr

+43-19
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,32 @@ LL - let _ = |(a): u8| 0;
7575
LL + let _ = |a: u8| 0;
7676
|
7777

78+
error: unnecessary parentheses around `if` condition
79+
--> $DIR/issue-54538-unused-parens-lint.rs:45:15
80+
|
81+
LL | loop { if (break) {} }
82+
| ^ ^
83+
|
84+
help: remove these parentheses
85+
|
86+
LL - loop { if (break) {} }
87+
LL + loop { if break {} }
88+
|
89+
90+
error: unnecessary parentheses around `if` condition
91+
--> $DIR/issue-54538-unused-parens-lint.rs:46:15
92+
|
93+
LL | loop { if (break ({ println!("hello") })) {} }
94+
| ^ ^
95+
|
96+
help: remove these parentheses
97+
|
98+
LL - loop { if (break ({ println!("hello") })) {} }
99+
LL + loop { if break ({ println!("hello") }) {} }
100+
|
101+
78102
error: unnecessary parentheses around pattern
79-
--> $DIR/issue-54538-unused-parens-lint.rs:56:12
103+
--> $DIR/issue-54538-unused-parens-lint.rs:71:12
80104
|
81105
LL | if let (0 | 1) = 0 {}
82106
| ^ ^
@@ -88,7 +112,7 @@ LL + if let 0 | 1 = 0 {}
88112
|
89113

90114
error: unnecessary parentheses around pattern
91-
--> $DIR/issue-54538-unused-parens-lint.rs:57:13
115+
--> $DIR/issue-54538-unused-parens-lint.rs:72:13
92116
|
93117
LL | if let ((0 | 1),) = (0,) {}
94118
| ^ ^
@@ -100,7 +124,7 @@ LL + if let (0 | 1,) = (0,) {}
100124
|
101125

102126
error: unnecessary parentheses around pattern
103-
--> $DIR/issue-54538-unused-parens-lint.rs:58:13
127+
--> $DIR/issue-54538-unused-parens-lint.rs:73:13
104128
|
105129
LL | if let [(0 | 1)] = [0] {}
106130
| ^ ^
@@ -112,7 +136,7 @@ LL + if let [0 | 1] = [0] {}
112136
|
113137

114138
error: unnecessary parentheses around pattern
115-
--> $DIR/issue-54538-unused-parens-lint.rs:59:16
139+
--> $DIR/issue-54538-unused-parens-lint.rs:74:16
116140
|
117141
LL | if let 0 | (1 | 2) = 0 {}
118142
| ^ ^
@@ -124,7 +148,7 @@ LL + if let 0 | 1 | 2 = 0 {}
124148
|
125149

126150
error: unnecessary parentheses around pattern
127-
--> $DIR/issue-54538-unused-parens-lint.rs:61:15
151+
--> $DIR/issue-54538-unused-parens-lint.rs:76:15
128152
|
129153
LL | if let TS((0 | 1)) = TS(0) {}
130154
| ^ ^
@@ -136,7 +160,7 @@ LL + if let TS(0 | 1) = TS(0) {}
136160
|
137161

138162
error: unnecessary parentheses around pattern
139-
--> $DIR/issue-54538-unused-parens-lint.rs:63:20
163+
--> $DIR/issue-54538-unused-parens-lint.rs:78:20
140164
|
141165
LL | if let NS { f: (0 | 1) } = (NS { f: 0 }) {}
142166
| ^ ^
@@ -148,7 +172,7 @@ LL + if let NS { f: 0 | 1 } = (NS { f: 0 }) {}
148172
|
149173

150174
error: unnecessary parentheses around pattern
151-
--> $DIR/issue-54538-unused-parens-lint.rs:73:9
175+
--> $DIR/issue-54538-unused-parens-lint.rs:88:9
152176
|
153177
LL | (_) => {}
154178
| ^ ^
@@ -160,7 +184,7 @@ LL + _ => {}
160184
|
161185

162186
error: unnecessary parentheses around pattern
163-
--> $DIR/issue-54538-unused-parens-lint.rs:74:9
187+
--> $DIR/issue-54538-unused-parens-lint.rs:89:9
164188
|
165189
LL | (y) => {}
166190
| ^ ^
@@ -172,7 +196,7 @@ LL + y => {}
172196
|
173197

174198
error: unnecessary parentheses around pattern
175-
--> $DIR/issue-54538-unused-parens-lint.rs:75:9
199+
--> $DIR/issue-54538-unused-parens-lint.rs:90:9
176200
|
177201
LL | (ref r) => {}
178202
| ^ ^
@@ -184,7 +208,7 @@ LL + ref r => {}
184208
|
185209

186210
error: unnecessary parentheses around pattern
187-
--> $DIR/issue-54538-unused-parens-lint.rs:76:9
211+
--> $DIR/issue-54538-unused-parens-lint.rs:91:9
188212
|
189213
LL | (e @ 1...2) => {}
190214
| ^ ^
@@ -196,7 +220,7 @@ LL + e @ 1...2 => {}
196220
|
197221

198222
error: unnecessary parentheses around pattern
199-
--> $DIR/issue-54538-unused-parens-lint.rs:82:9
223+
--> $DIR/issue-54538-unused-parens-lint.rs:97:9
200224
|
201225
LL | (e @ &(1...2)) => {}
202226
| ^ ^
@@ -208,7 +232,7 @@ LL + e @ &(1...2) => {}
208232
|
209233

210234
error: unnecessary parentheses around pattern
211-
--> $DIR/issue-54538-unused-parens-lint.rs:83:10
235+
--> $DIR/issue-54538-unused-parens-lint.rs:98:10
212236
|
213237
LL | &(_) => {}
214238
| ^ ^
@@ -220,7 +244,7 @@ LL + &_ => {}
220244
|
221245

222246
error: unnecessary parentheses around pattern
223-
--> $DIR/issue-54538-unused-parens-lint.rs:94:9
247+
--> $DIR/issue-54538-unused-parens-lint.rs:109:9
224248
|
225249
LL | (_) => {}
226250
| ^ ^
@@ -232,7 +256,7 @@ LL + _ => {}
232256
|
233257

234258
error: unnecessary parentheses around pattern
235-
--> $DIR/issue-54538-unused-parens-lint.rs:95:9
259+
--> $DIR/issue-54538-unused-parens-lint.rs:110:9
236260
|
237261
LL | (y) => {}
238262
| ^ ^
@@ -244,7 +268,7 @@ LL + y => {}
244268
|
245269

246270
error: unnecessary parentheses around pattern
247-
--> $DIR/issue-54538-unused-parens-lint.rs:96:9
271+
--> $DIR/issue-54538-unused-parens-lint.rs:111:9
248272
|
249273
LL | (ref r) => {}
250274
| ^ ^
@@ -256,7 +280,7 @@ LL + ref r => {}
256280
|
257281

258282
error: unnecessary parentheses around pattern
259-
--> $DIR/issue-54538-unused-parens-lint.rs:97:9
283+
--> $DIR/issue-54538-unused-parens-lint.rs:112:9
260284
|
261285
LL | (e @ 1..=2) => {}
262286
| ^ ^
@@ -268,7 +292,7 @@ LL + e @ 1..=2 => {}
268292
|
269293

270294
error: unnecessary parentheses around pattern
271-
--> $DIR/issue-54538-unused-parens-lint.rs:103:9
295+
--> $DIR/issue-54538-unused-parens-lint.rs:118:9
272296
|
273297
LL | (e @ &(1..=2)) => {}
274298
| ^ ^
@@ -280,7 +304,7 @@ LL + e @ &(1..=2) => {}
280304
|
281305

282306
error: unnecessary parentheses around pattern
283-
--> $DIR/issue-54538-unused-parens-lint.rs:104:10
307+
--> $DIR/issue-54538-unused-parens-lint.rs:119:10
284308
|
285309
LL | &(_) => {}
286310
| ^ ^
@@ -291,5 +315,5 @@ LL - &(_) => {}
291315
LL + &_ => {}
292316
|
293317

294-
error: aborting due to 24 previous errors
318+
error: aborting due to 26 previous errors
295319

0 commit comments

Comments
 (0)