From 3471bc8306e206ed7d5cc86b1eeadc9a8bb8bc76 Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Tue, 5 May 2020 01:55:03 +0200 Subject: [PATCH 1/3] Fix unused_parens false positive when using binary operations --- src/librustc_lint/unused.rs | 16 ++++++++++++---- .../ui/lint/issue-71290-unused-paren-binop.rs | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/lint/issue-71290-unused-paren-binop.rs diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index ddd252cb290e4..4f7a1f9c31091 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -380,11 +380,19 @@ trait UnusedDelimLint { ); fn is_expr_delims_necessary(inner: &ast::Expr, followed_by_block: bool) -> bool { - followed_by_block - && match inner.kind { - ExprKind::Ret(_) | ExprKind::Break(..) => true, - _ => parser::contains_exterior_struct_lit(&inner), + // Prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }` + let lhs_needs_parens = match &inner.kind { + ExprKind::Binary(_, lhs, _rhs) => { + !rustc_ast::util::classify::expr_requires_semi_to_be_stmt(&*lhs) } + _ => false, + }; + lhs_needs_parens + || (followed_by_block + && match inner.kind { + ExprKind::Ret(_) | ExprKind::Break(..) => true, + _ => parser::contains_exterior_struct_lit(&inner), + }) } fn emit_unused_delims_expr( diff --git a/src/test/ui/lint/issue-71290-unused-paren-binop.rs b/src/test/ui/lint/issue-71290-unused-paren-binop.rs new file mode 100644 index 0000000000000..619be02566f56 --- /dev/null +++ b/src/test/ui/lint/issue-71290-unused-paren-binop.rs @@ -0,0 +1,17 @@ +// check-pass +// Make sure unused parens lint doesn't emit a false positive. +// See https://github.com/rust-lang/rust/issues/71290 for details. + +fn x() -> u8 { + ({ 0 }) + 1 +} + +fn y() -> u8 { + ({ 0 } + 1) +} + +pub fn foo(a: bool, b: bool) -> u8 { + (if a { 1 } else { 0 } + if b { 1 } else { 0 }) +} + +fn main() {} From 717a7a8486a448d7c746db213d1d987593088692 Mon Sep 17 00:00:00 2001 From: Michael <5672750+mibac138@users.noreply.github.com> Date: Tue, 5 May 2020 16:58:13 +0200 Subject: [PATCH 2/3] Update src/test/ui/lint/issue-71290-unused-paren-binop.rs Co-authored-by: Bastian Kauschke --- src/test/ui/lint/issue-71290-unused-paren-binop.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/ui/lint/issue-71290-unused-paren-binop.rs b/src/test/ui/lint/issue-71290-unused-paren-binop.rs index 619be02566f56..f11d2d377378c 100644 --- a/src/test/ui/lint/issue-71290-unused-paren-binop.rs +++ b/src/test/ui/lint/issue-71290-unused-paren-binop.rs @@ -1,6 +1,7 @@ // check-pass // Make sure unused parens lint doesn't emit a false positive. // See https://github.com/rust-lang/rust/issues/71290 for details. +#![deny(unused_parens)] fn x() -> u8 { ({ 0 }) + 1 From 4b7a92838c98929a9f4c7a64583b5ea49ebb3a35 Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Wed, 6 May 2020 01:50:55 +0200 Subject: [PATCH 3/3] Fix unused_parens nested binary op false positive --- src/librustc_lint/unused.rs | 16 ++++++++++++---- .../ui/lint/issue-71290-unused-paren-binop.rs | 5 +++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 4f7a1f9c31091..c24079a6e4be2 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -381,12 +381,20 @@ trait UnusedDelimLint { fn is_expr_delims_necessary(inner: &ast::Expr, followed_by_block: bool) -> bool { // Prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }` - let lhs_needs_parens = match &inner.kind { - ExprKind::Binary(_, lhs, _rhs) => { - !rustc_ast::util::classify::expr_requires_semi_to_be_stmt(&*lhs) + let lhs_needs_parens = { + let mut innermost = inner; + loop { + if let ExprKind::Binary(_, lhs, _rhs) = &innermost.kind { + innermost = lhs; + if !rustc_ast::util::classify::expr_requires_semi_to_be_stmt(innermost) { + break true; + } + } else { + break false; + } } - _ => false, }; + lhs_needs_parens || (followed_by_block && match inner.kind { diff --git a/src/test/ui/lint/issue-71290-unused-paren-binop.rs b/src/test/ui/lint/issue-71290-unused-paren-binop.rs index f11d2d377378c..24d77e36d94f5 100644 --- a/src/test/ui/lint/issue-71290-unused-paren-binop.rs +++ b/src/test/ui/lint/issue-71290-unused-paren-binop.rs @@ -15,4 +15,9 @@ pub fn foo(a: bool, b: bool) -> u8 { (if a { 1 } else { 0 } + if b { 1 } else { 0 }) } +pub fn bar() -> u8 { + // Make sure nested expressions are handled correctly as well + ({ 0 } + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) +} + fn main() {}