Skip to content

Commit 4891531

Browse files
authored
Rollup merge of #91956 - notriddle:notriddle/unused-parens-range, r=nagisa
fix(rustc_lint): better detect when parens are necessary Fixes #90807
2 parents 6b62bf3 + f4a0321 commit 4891531

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

compiler/rustc_lint/src/unused.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,11 @@ trait UnusedDelimLint {
478478

479479
lhs_needs_parens
480480
|| (followed_by_block
481-
&& match inner.kind {
481+
&& match &inner.kind {
482482
ExprKind::Ret(_) | ExprKind::Break(..) | ExprKind::Yield(..) => true,
483+
ExprKind::Range(_lhs, Some(rhs), _limits) => {
484+
matches!(rhs.kind, ExprKind::Block(..))
485+
}
483486
_ => parser::contains_exterior_struct_lit(&inner),
484487
})
485488
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Make sure unused parens lint emit is emitted for loop and match.
2+
// See https://github.com/rust-lang/rust/issues/90807
3+
// and https://github.com/rust-lang/rust/pull/91956#discussion_r771647953
4+
#![deny(unused_parens)]
5+
6+
fn main() {
7+
for _ in (1..loop { break 2 }) {} //~ERROR
8+
for _ in (1..match () { () => 2 }) {} //~ERROR
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: unnecessary parentheses around `for` iterator expression
2+
--> $DIR/issue-90807-unused-paren-error.rs:7:14
3+
|
4+
LL | for _ in (1..loop { break 2 }) {}
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-90807-unused-paren-error.rs:4:9
9+
|
10+
LL | #![deny(unused_parens)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these parentheses
13+
|
14+
LL - for _ in (1..loop { break 2 }) {}
15+
LL + for _ in 1..loop { break 2 } {}
16+
|
17+
18+
error: unnecessary parentheses around `for` iterator expression
19+
--> $DIR/issue-90807-unused-paren-error.rs:8:14
20+
|
21+
LL | for _ in (1..match () { () => 2 }) {}
22+
| ^ ^
23+
|
24+
help: remove these parentheses
25+
|
26+
LL - for _ in (1..match () { () => 2 }) {}
27+
LL + for _ in 1..match () { () => 2 } {}
28+
|
29+
30+
error: aborting due to 2 previous errors
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// check-pass
2+
// Make sure unused parens lint doesn't emit a false positive.
3+
// See https://github.com/rust-lang/rust/issues/90807
4+
#![deny(unused_parens)]
5+
6+
fn main() {
7+
for _ in (1..{ 2 }) {}
8+
}

0 commit comments

Comments
 (0)