Fix unused_parens false positive when using binary operations
This commit is contained in:
parent
d626e4dadc
commit
3471bc8306
2 changed files with 29 additions and 4 deletions
|
@ -380,11 +380,19 @@ trait UnusedDelimLint {
|
||||||
);
|
);
|
||||||
|
|
||||||
fn is_expr_delims_necessary(inner: &ast::Expr, followed_by_block: bool) -> bool {
|
fn is_expr_delims_necessary(inner: &ast::Expr, followed_by_block: bool) -> bool {
|
||||||
followed_by_block
|
// Prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`
|
||||||
&& match inner.kind {
|
let lhs_needs_parens = match &inner.kind {
|
||||||
ExprKind::Ret(_) | ExprKind::Break(..) => true,
|
ExprKind::Binary(_, lhs, _rhs) => {
|
||||||
_ => parser::contains_exterior_struct_lit(&inner),
|
!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(
|
fn emit_unused_delims_expr(
|
||||||
|
|
17
src/test/ui/lint/issue-71290-unused-paren-binop.rs
Normal file
17
src/test/ui/lint/issue-71290-unused-paren-binop.rs
Normal file
|
@ -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() {}
|
Loading…
Add table
Add a link
Reference in a new issue