Skip to content

Commit 1f83a4d

Browse files
Rollup merge of #132936 - surechen:fix_131989, r=Nadrieril
For expr `return (_ = 42);` unused_paren lint should not be triggered fixes #131989
2 parents 917a50a + 3a74bce commit 1f83a4d

13 files changed

+189
-40
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ enum UnusedDelimsCtx {
584584
MatchScrutineeExpr,
585585
ReturnValue,
586586
BlockRetValue,
587+
BreakValue,
587588
LetScrutineeExpr,
588589
ArrayLenExpr,
589590
AnonConst,
@@ -605,6 +606,7 @@ impl From<UnusedDelimsCtx> for &'static str {
605606
UnusedDelimsCtx::MatchScrutineeExpr => "`match` scrutinee expression",
606607
UnusedDelimsCtx::ReturnValue => "`return` value",
607608
UnusedDelimsCtx::BlockRetValue => "block return value",
609+
UnusedDelimsCtx::BreakValue => "`break` value",
608610
UnusedDelimsCtx::LetScrutineeExpr => "`let` scrutinee expression",
609611
UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression",
610612
UnusedDelimsCtx::MatchArmExpr => "match arm expression",
@@ -913,6 +915,10 @@ trait UnusedDelimLint {
913915
(value, UnusedDelimsCtx::ReturnValue, false, Some(left), None, true)
914916
}
915917

918+
Break(_, Some(ref value)) => {
919+
(value, UnusedDelimsCtx::BreakValue, false, None, None, true)
920+
}
921+
916922
Index(_, ref value, _) => (value, UnusedDelimsCtx::IndexExpr, false, None, None, false),
917923

918924
Assign(_, ref value, _) | AssignOp(.., ref value) => {
@@ -1063,6 +1069,9 @@ impl UnusedDelimLint for UnusedParens {
10631069
_,
10641070
_,
10651071
) if node.is_lazy()))
1072+
&& !((ctx == UnusedDelimsCtx::ReturnValue
1073+
|| ctx == UnusedDelimsCtx::BreakValue)
1074+
&& matches!(inner.kind, ast::ExprKind::Assign(_, _, _)))
10661075
{
10671076
self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
10681077
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
unused_mut,
1111
unused_variables
1212
)]
13-
#![deny(unused_parens)]
13+
#![deny(unused_parens, unused_braces)]
1414

1515
fn lint_on_top_level() {
1616
let a = 0; //~ ERROR unnecessary parentheses around pattern
@@ -43,8 +43,10 @@ fn no_lint_ops() {
4343
fn lint_break_if_not_followed_by_block() {
4444
#![allow(unreachable_code)]
4545
loop { if break {} } //~ ERROR unnecessary parentheses
46-
loop { if break ({ println!("hello") }) {} } //~ ERROR unnecessary parentheses
47-
loop { if (break { println!("hello") }) {} }
46+
loop { if break { println!("hello") } {} }
47+
//~^ ERROR unnecessary parentheses around `if` condition
48+
//~| ERROR unnecessary parentheses around `break` value
49+
loop { if (break println!("hello")) {} } //~ ERROR unnecessary braces around `break` value
4850
}
4951

5052
// Don't lint in these cases (#64106).

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
unused_mut,
1111
unused_variables
1212
)]
13-
#![deny(unused_parens)]
13+
#![deny(unused_parens, unused_braces)]
1414

1515
fn lint_on_top_level() {
1616
let (a) = 0; //~ ERROR unnecessary parentheses around pattern
@@ -43,8 +43,10 @@ fn no_lint_ops() {
4343
fn lint_break_if_not_followed_by_block() {
4444
#![allow(unreachable_code)]
4545
loop { if (break) {} } //~ ERROR unnecessary parentheses
46-
loop { if (break ({ println!("hello") })) {} } //~ ERROR unnecessary parentheses
47-
loop { if (break { println!("hello") }) {} }
46+
loop { if (break ({ println!("hello") })) {} }
47+
//~^ ERROR unnecessary parentheses around `if` condition
48+
//~| ERROR unnecessary parentheses around `break` value
49+
loop { if (break { println!("hello") }) {} } //~ ERROR unnecessary braces around `break` value
4850
}
4951

5052
// Don't lint in these cases (#64106).

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

+49-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let (a) = 0;
77
note: the lint level is defined here
88
--> $DIR/issue-54538-unused-parens-lint.rs:13:9
99
|
10-
LL | #![deny(unused_parens)]
10+
LL | #![deny(unused_parens, unused_braces)]
1111
| ^^^^^^^^^^^^^
1212
help: remove these parentheses
1313
|
@@ -99,8 +99,37 @@ LL - loop { if (break ({ println!("hello") })) {} }
9999
LL + loop { if break ({ println!("hello") }) {} }
100100
|
101101

102+
error: unnecessary parentheses around `break` value
103+
--> $DIR/issue-54538-unused-parens-lint.rs:46:22
104+
|
105+
LL | loop { if (break ({ println!("hello") })) {} }
106+
| ^ ^
107+
|
108+
help: remove these parentheses
109+
|
110+
LL - loop { if (break ({ println!("hello") })) {} }
111+
LL + loop { if (break { println!("hello") }) {} }
112+
|
113+
114+
error: unnecessary braces around `break` value
115+
--> $DIR/issue-54538-unused-parens-lint.rs:49:22
116+
|
117+
LL | loop { if (break { println!("hello") }) {} }
118+
| ^^ ^^
119+
|
120+
note: the lint level is defined here
121+
--> $DIR/issue-54538-unused-parens-lint.rs:13:24
122+
|
123+
LL | #![deny(unused_parens, unused_braces)]
124+
| ^^^^^^^^^^^^^
125+
help: remove these braces
126+
|
127+
LL - loop { if (break { println!("hello") }) {} }
128+
LL + loop { if (break println!("hello")) {} }
129+
|
130+
102131
error: unnecessary parentheses around pattern
103-
--> $DIR/issue-54538-unused-parens-lint.rs:71:12
132+
--> $DIR/issue-54538-unused-parens-lint.rs:73:12
104133
|
105134
LL | if let (0 | 1) = 0 {}
106135
| ^ ^
@@ -112,7 +141,7 @@ LL + if let 0 | 1 = 0 {}
112141
|
113142

114143
error: unnecessary parentheses around pattern
115-
--> $DIR/issue-54538-unused-parens-lint.rs:72:13
144+
--> $DIR/issue-54538-unused-parens-lint.rs:74:13
116145
|
117146
LL | if let ((0 | 1),) = (0,) {}
118147
| ^ ^
@@ -124,7 +153,7 @@ LL + if let (0 | 1,) = (0,) {}
124153
|
125154

126155
error: unnecessary parentheses around pattern
127-
--> $DIR/issue-54538-unused-parens-lint.rs:73:13
156+
--> $DIR/issue-54538-unused-parens-lint.rs:75:13
128157
|
129158
LL | if let [(0 | 1)] = [0] {}
130159
| ^ ^
@@ -136,7 +165,7 @@ LL + if let [0 | 1] = [0] {}
136165
|
137166

138167
error: unnecessary parentheses around pattern
139-
--> $DIR/issue-54538-unused-parens-lint.rs:74:16
168+
--> $DIR/issue-54538-unused-parens-lint.rs:76:16
140169
|
141170
LL | if let 0 | (1 | 2) = 0 {}
142171
| ^ ^
@@ -148,7 +177,7 @@ LL + if let 0 | 1 | 2 = 0 {}
148177
|
149178

150179
error: unnecessary parentheses around pattern
151-
--> $DIR/issue-54538-unused-parens-lint.rs:76:15
180+
--> $DIR/issue-54538-unused-parens-lint.rs:78:15
152181
|
153182
LL | if let TS((0 | 1)) = TS(0) {}
154183
| ^ ^
@@ -160,7 +189,7 @@ LL + if let TS(0 | 1) = TS(0) {}
160189
|
161190

162191
error: unnecessary parentheses around pattern
163-
--> $DIR/issue-54538-unused-parens-lint.rs:78:20
192+
--> $DIR/issue-54538-unused-parens-lint.rs:80:20
164193
|
165194
LL | if let NS { f: (0 | 1) } = (NS { f: 0 }) {}
166195
| ^ ^
@@ -172,7 +201,7 @@ LL + if let NS { f: 0 | 1 } = (NS { f: 0 }) {}
172201
|
173202

174203
error: unnecessary parentheses around pattern
175-
--> $DIR/issue-54538-unused-parens-lint.rs:88:9
204+
--> $DIR/issue-54538-unused-parens-lint.rs:90:9
176205
|
177206
LL | (_) => {}
178207
| ^ ^
@@ -184,7 +213,7 @@ LL + _ => {}
184213
|
185214

186215
error: unnecessary parentheses around pattern
187-
--> $DIR/issue-54538-unused-parens-lint.rs:89:9
216+
--> $DIR/issue-54538-unused-parens-lint.rs:91:9
188217
|
189218
LL | (y) => {}
190219
| ^ ^
@@ -196,7 +225,7 @@ LL + y => {}
196225
|
197226

198227
error: unnecessary parentheses around pattern
199-
--> $DIR/issue-54538-unused-parens-lint.rs:90:9
228+
--> $DIR/issue-54538-unused-parens-lint.rs:92:9
200229
|
201230
LL | (ref r) => {}
202231
| ^ ^
@@ -208,7 +237,7 @@ LL + ref r => {}
208237
|
209238

210239
error: unnecessary parentheses around pattern
211-
--> $DIR/issue-54538-unused-parens-lint.rs:91:9
240+
--> $DIR/issue-54538-unused-parens-lint.rs:93:9
212241
|
213242
LL | (e @ 1...2) => {}
214243
| ^ ^
@@ -220,7 +249,7 @@ LL + e @ 1...2 => {}
220249
|
221250

222251
error: unnecessary parentheses around pattern
223-
--> $DIR/issue-54538-unused-parens-lint.rs:97:9
252+
--> $DIR/issue-54538-unused-parens-lint.rs:99:9
224253
|
225254
LL | (e @ &(1...2)) => {}
226255
| ^ ^
@@ -232,7 +261,7 @@ LL + e @ &(1...2) => {}
232261
|
233262

234263
error: unnecessary parentheses around pattern
235-
--> $DIR/issue-54538-unused-parens-lint.rs:98:10
264+
--> $DIR/issue-54538-unused-parens-lint.rs:100:10
236265
|
237266
LL | &(_) => {}
238267
| ^ ^
@@ -244,7 +273,7 @@ LL + &_ => {}
244273
|
245274

246275
error: unnecessary parentheses around pattern
247-
--> $DIR/issue-54538-unused-parens-lint.rs:109:9
276+
--> $DIR/issue-54538-unused-parens-lint.rs:111:9
248277
|
249278
LL | (_) => {}
250279
| ^ ^
@@ -256,7 +285,7 @@ LL + _ => {}
256285
|
257286

258287
error: unnecessary parentheses around pattern
259-
--> $DIR/issue-54538-unused-parens-lint.rs:110:9
288+
--> $DIR/issue-54538-unused-parens-lint.rs:112:9
260289
|
261290
LL | (y) => {}
262291
| ^ ^
@@ -268,7 +297,7 @@ LL + y => {}
268297
|
269298

270299
error: unnecessary parentheses around pattern
271-
--> $DIR/issue-54538-unused-parens-lint.rs:111:9
300+
--> $DIR/issue-54538-unused-parens-lint.rs:113:9
272301
|
273302
LL | (ref r) => {}
274303
| ^ ^
@@ -280,7 +309,7 @@ LL + ref r => {}
280309
|
281310

282311
error: unnecessary parentheses around pattern
283-
--> $DIR/issue-54538-unused-parens-lint.rs:112:9
312+
--> $DIR/issue-54538-unused-parens-lint.rs:114:9
284313
|
285314
LL | (e @ 1..=2) => {}
286315
| ^ ^
@@ -292,7 +321,7 @@ LL + e @ 1..=2 => {}
292321
|
293322

294323
error: unnecessary parentheses around pattern
295-
--> $DIR/issue-54538-unused-parens-lint.rs:118:9
324+
--> $DIR/issue-54538-unused-parens-lint.rs:120:9
296325
|
297326
LL | (e @ &(1..=2)) => {}
298327
| ^ ^
@@ -304,7 +333,7 @@ LL + e @ &(1..=2) => {}
304333
|
305334

306335
error: unnecessary parentheses around pattern
307-
--> $DIR/issue-54538-unused-parens-lint.rs:119:10
336+
--> $DIR/issue-54538-unused-parens-lint.rs:121:10
308337
|
309338
LL | &(_) => {}
310339
| ^ ^
@@ -315,5 +344,5 @@ LL - &(_) => {}
315344
LL + &_ => {}
316345
|
317346

318-
error: aborting due to 26 previous errors
347+
error: aborting due to 28 previous errors
319348

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//@ run-rustfix
2+
#![deny(unused_parens)]
3+
#![allow(unreachable_code)]
4+
5+
fn foo() {
6+
loop {
7+
break (_ = 42);
8+
// lint unused_parens should not be triggered here.
9+
}
10+
11+
let _ = loop {
12+
let a = 1;
13+
let b = 2;
14+
break a + b; //~ERROR unnecessary parentheses
15+
};
16+
17+
loop {
18+
if break return () {
19+
//~^ ERROR unnecessary parentheses
20+
}
21+
if break return () {
22+
//~^ ERROR unnecessary parentheses
23+
}
24+
}
25+
26+
return (_ = 42);
27+
// lint unused_parens should not be triggered here.
28+
}
29+
30+
fn main() {
31+
let _ = foo();
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//@ run-rustfix
2+
#![deny(unused_parens)]
3+
#![allow(unreachable_code)]
4+
5+
fn foo() {
6+
loop {
7+
break (_ = 42);
8+
// lint unused_parens should not be triggered here.
9+
}
10+
11+
let _ = loop {
12+
let a = 1;
13+
let b = 2;
14+
break (a + b); //~ERROR unnecessary parentheses
15+
};
16+
17+
loop {
18+
if (break return ()) {
19+
//~^ ERROR unnecessary parentheses
20+
}
21+
if break (return ()) {
22+
//~^ ERROR unnecessary parentheses
23+
}
24+
}
25+
26+
return (_ = 42);
27+
// lint unused_parens should not be triggered here.
28+
}
29+
30+
fn main() {
31+
let _ = foo();
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error: unnecessary parentheses around `break` value
2+
--> $DIR/unused-parens-assign-expr-in-ret-issue-131989.rs:14:15
3+
|
4+
LL | break (a + b);
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-parens-assign-expr-in-ret-issue-131989.rs:2:9
9+
|
10+
LL | #![deny(unused_parens)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these parentheses
13+
|
14+
LL - break (a + b);
15+
LL + break a + b;
16+
|
17+
18+
error: unnecessary parentheses around `if` condition
19+
--> $DIR/unused-parens-assign-expr-in-ret-issue-131989.rs:18:12
20+
|
21+
LL | if (break return ()) {
22+
| ^ ^
23+
|
24+
help: remove these parentheses
25+
|
26+
LL - if (break return ()) {
27+
LL + if break return () {
28+
|
29+
30+
error: unnecessary parentheses around `break` value
31+
--> $DIR/unused-parens-assign-expr-in-ret-issue-131989.rs:21:18
32+
|
33+
LL | if break (return ()) {
34+
| ^ ^
35+
|
36+
help: remove these parentheses
37+
|
38+
LL - if break (return ()) {
39+
LL + if break return () {
40+
|
41+
42+
error: aborting due to 3 previous errors
43+

0 commit comments

Comments
 (0)