Skip to content

Commit 4b7a928

Browse files
committed
Fix unused_parens nested binary op false positive
1 parent 717a7a8 commit 4b7a928

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/librustc_lint/unused.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,20 @@ trait UnusedDelimLint {
381381

382382
fn is_expr_delims_necessary(inner: &ast::Expr, followed_by_block: bool) -> bool {
383383
// Prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`
384-
let lhs_needs_parens = match &inner.kind {
385-
ExprKind::Binary(_, lhs, _rhs) => {
386-
!rustc_ast::util::classify::expr_requires_semi_to_be_stmt(&*lhs)
384+
let lhs_needs_parens = {
385+
let mut innermost = inner;
386+
loop {
387+
if let ExprKind::Binary(_, lhs, _rhs) = &innermost.kind {
388+
innermost = lhs;
389+
if !rustc_ast::util::classify::expr_requires_semi_to_be_stmt(innermost) {
390+
break true;
391+
}
392+
} else {
393+
break false;
394+
}
387395
}
388-
_ => false,
389396
};
397+
390398
lhs_needs_parens
391399
|| (followed_by_block
392400
&& match inner.kind {

src/test/ui/lint/issue-71290-unused-paren-binop.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ pub fn foo(a: bool, b: bool) -> u8 {
1515
(if a { 1 } else { 0 } + if b { 1 } else { 0 })
1616
}
1717

18+
pub fn bar() -> u8 {
19+
// Make sure nested expressions are handled correctly as well
20+
({ 0 } + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9)
21+
}
22+
1823
fn main() {}

0 commit comments

Comments
 (0)