Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rustc_lint): better detect when parens are necessary #91956

Merged
merged 2 commits into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,11 @@ trait UnusedDelimLint {

lhs_needs_parens
|| (followed_by_block
&& match inner.kind {
&& match &inner.kind {
ExprKind::Ret(_) | ExprKind::Break(..) | ExprKind::Yield(..) => true,
ExprKind::Range(_lhs, Some(rhs), _limits) => {
matches!(rhs.kind, ExprKind::Block(..))
}
_ => parser::contains_exterior_struct_lit(&inner),
})
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/lint/unused/issue-90807-unused-paren-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Make sure unused parens lint emit is emitted for loop and match.
// See https://github.com/rust-lang/rust/issues/90807
// and https://github.com/rust-lang/rust/pull/91956#discussion_r771647953
#![deny(unused_parens)]

fn main() {
for _ in (1..loop { break 2 }) {} //~ERROR
for _ in (1..match () { () => 2 }) {} //~ERROR
}
31 changes: 31 additions & 0 deletions src/test/ui/lint/unused/issue-90807-unused-paren-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error: unnecessary parentheses around `for` iterator expression
--> $DIR/issue-90807-unused-paren-error.rs:7:14
|
LL | for _ in (1..loop { break 2 }) {}
| ^ ^
|
note: the lint level is defined here
--> $DIR/issue-90807-unused-paren-error.rs:4:9
|
LL | #![deny(unused_parens)]
| ^^^^^^^^^^^^^
help: remove these parentheses
|
LL - for _ in (1..loop { break 2 }) {}
LL + for _ in 1..loop { break 2 } {}
|

error: unnecessary parentheses around `for` iterator expression
--> $DIR/issue-90807-unused-paren-error.rs:8:14
|
LL | for _ in (1..match () { () => 2 }) {}
| ^ ^
|
help: remove these parentheses
|
LL - for _ in (1..match () { () => 2 }) {}
LL + for _ in 1..match () { () => 2 } {}
|

error: aborting due to 2 previous errors

8 changes: 8 additions & 0 deletions src/test/ui/lint/unused/issue-90807-unused-paren.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-pass
// Make sure unused parens lint doesn't emit a false positive.
// See https://github.com/rust-lang/rust/issues/90807
#![deny(unused_parens)]

fn main() {
for _ in (1..{ 2 }) {}
notriddle marked this conversation as resolved.
Show resolved Hide resolved
}