Fix unused_parens nested binary op false positive

This commit is contained in:
mibac138 2020-05-06 01:50:55 +02:00
parent 717a7a8486
commit 4b7a92838c
2 changed files with 17 additions and 4 deletions

View File

@ -381,12 +381,20 @@ fn check_unused_delims_expr(
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 {

View File

@ -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() {}