Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unused_parens false positive when using binary operations #71910

Merged
merged 3 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/lint/issue-71290-unused-paren-binop.rs
Original file line number Diff line number Diff line change
@@ -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.
mibac138 marked this conversation as resolved.
Show resolved Hide resolved

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