Skip to content

Commit 3471bc8

Browse files
committed
Fix unused_parens false positive when using binary operations
1 parent d626e4d commit 3471bc8

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/librustc_lint/unused.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,19 @@ trait UnusedDelimLint {
380380
);
381381

382382
fn is_expr_delims_necessary(inner: &ast::Expr, followed_by_block: bool) -> bool {
383-
followed_by_block
384-
&& match inner.kind {
385-
ExprKind::Ret(_) | ExprKind::Break(..) => true,
386-
_ => parser::contains_exterior_struct_lit(&inner),
383+
// 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)
387387
}
388+
_ => false,
389+
};
390+
lhs_needs_parens
391+
|| (followed_by_block
392+
&& match inner.kind {
393+
ExprKind::Ret(_) | ExprKind::Break(..) => true,
394+
_ => parser::contains_exterior_struct_lit(&inner),
395+
})
388396
}
389397

390398
fn emit_unused_delims_expr(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-pass
2+
// Make sure unused parens lint doesn't emit a false positive.
3+
// See https://github.com/rust-lang/rust/issues/71290 for details.
4+
5+
fn x() -> u8 {
6+
({ 0 }) + 1
7+
}
8+
9+
fn y() -> u8 {
10+
({ 0 } + 1)
11+
}
12+
13+
pub fn foo(a: bool, b: bool) -> u8 {
14+
(if a { 1 } else { 0 } + if b { 1 } else { 0 })
15+
}
16+
17+
fn main() {}

0 commit comments

Comments
 (0)