diff options
| author | mibac138 <5672750+mibac138@users.noreply.github.com> | 2020-05-05 01:55:03 +0200 |
|---|---|---|
| committer | mibac138 <5672750+mibac138@users.noreply.github.com> | 2020-05-05 02:21:50 +0200 |
| commit | 3471bc8306e206ed7d5cc86b1eeadc9a8bb8bc76 (patch) | |
| tree | da17311f903cf0cf509ef2e7e5de9a789b2df672 | |
| parent | d626e4dadc37d7027d65f087da0ad1ddb460959f (diff) | |
| download | rust-3471bc8306e206ed7d5cc86b1eeadc9a8bb8bc76.tar.gz rust-3471bc8306e206ed7d5cc86b1eeadc9a8bb8bc76.zip | |
Fix unused_parens false positive when using binary operations
| -rw-r--r-- | src/librustc_lint/unused.rs | 16 | ||||
| -rw-r--r-- | src/test/ui/lint/issue-71290-unused-paren-binop.rs | 17 |
2 files changed, 29 insertions, 4 deletions
diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index ddd252cb290..4f7a1f9c310 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 00000000000..619be02566f --- /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() {} |
