From 39867f3c9fea0e01b5b199d8c2b6b0889284cc85 Mon Sep 17 00:00:00 2001 From: aticu <15schnic@gmail.com> Date: Wed, 14 Oct 2020 01:13:48 +0200 Subject: [PATCH] Fixed false positive for `unused_parens` lint --- compiler/rustc_lint/src/unused.rs | 11 +++++++++-- src/test/ui/lint/lint-unnecessary-parens.fixed | 2 ++ src/test/ui/lint/lint-unnecessary-parens.rs | 2 ++ src/test/ui/lint/lint-unnecessary-parens.stderr | 8 ++++---- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 3abd9a6325d6e..51ec599493399 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -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, diff --git a/src/test/ui/lint/lint-unnecessary-parens.fixed b/src/test/ui/lint/lint-unnecessary-parens.fixed index c9dec395580f1..9c144324f2f7e 100644 --- a/src/test/ui/lint/lint-unnecessary-parens.fixed +++ b/src/test/ui/lint/lint-unnecessary-parens.fixed @@ -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) {} diff --git a/src/test/ui/lint/lint-unnecessary-parens.rs b/src/test/ui/lint/lint-unnecessary-parens.rs index 884bb4d2e99b6..4fd9cabb3b0b2 100644 --- a/src/test/ui/lint/lint-unnecessary-parens.rs +++ b/src/test/ui/lint/lint-unnecessary-parens.rs @@ -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) {} diff --git a/src/test/ui/lint/lint-unnecessary-parens.stderr b/src/test/ui/lint/lint-unnecessary-parens.stderr index 1abf47c8af521..9eae7da90047e 100644 --- a/src/test/ui/lint/lint-unnecessary-parens.stderr +++ b/src/test/ui/lint/lint-unnecessary-parens.stderr @@ -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