Skip to content

Commit

Permalink
Rollup merge of #77931 - aticu:fix_60336, r=petrochenkov
Browse files Browse the repository at this point in the history
Fix false positive for `unused_parens` lint

Fixes #60336
  • Loading branch information
JohnTitor authored Oct 20, 2020
2 parents 6df79bf + 39867f3 commit 378ca5e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
11 changes: 9 additions & 2 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,13 +751,20 @@ impl UnusedDelimLint for UnusedParens {
if !Self::is_expr_delims_necessary(inner, followed_by_block)
&& value.attrs.is_empty()
&& !value.span.from_expansion()
&& (ctx != UnusedDelimsCtx::LetScrutineeExpr
|| match inner.kind {
ast::ExprKind::Binary(
rustc_span::source_map::Spanned { node, .. },
_,
_,
) if node.lazy() => false,
_ => true,
})
{
self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos)
}
}
ast::ExprKind::Let(_, ref expr) => {
// FIXME(#60336): Properly handle `let true = (false && true)`
// actually needing the parenthesis.
self.check_unused_delims_expr(
cx,
expr,
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/lint/lint-unnecessary-parens.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ fn main() {
if (v == X { y: true }) {}
if (X { y: true } == v) {}
if (X { y: false }.y) {}
// this shouldn't warn, because the parens are necessary to disambiguate let chains
if let true = (true && false) {}

while (X { y: false }.foo(true)) {}
while (true | X { y: false }.y) {}
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/lint/lint-unnecessary-parens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ fn main() {
if (v == X { y: true }) {}
if (X { y: true } == v) {}
if (X { y: false }.y) {}
// this shouldn't warn, because the parens are necessary to disambiguate let chains
if let true = (true && false) {}

while (X { y: false }.foo(true)) {}
while (true | X { y: false }.y) {}
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/lint/lint-unnecessary-parens.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ LL | while let 1 = (2) {}
| ^^^ help: remove these parentheses

error: unnecessary parentheses around method argument
--> $DIR/lint-unnecessary-parens.rs:71:24
--> $DIR/lint-unnecessary-parens.rs:73:24
|
LL | X { y: false }.foo((true));
| ^^^^^^ help: remove these parentheses

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:73:18
--> $DIR/lint-unnecessary-parens.rs:75:18
|
LL | let mut _a = (0);
| ^^^ help: remove these parentheses

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:74:10
--> $DIR/lint-unnecessary-parens.rs:76:10
|
LL | _a = (0);
| ^^^ help: remove these parentheses

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:75:11
--> $DIR/lint-unnecessary-parens.rs:77:11
|
LL | _a += (1);
| ^^^ help: remove these parentheses
Expand Down

0 comments on commit 378ca5e

Please sign in to comment.