fix: don't remove parentheses for calls of function-like pointers that are members of a struct or union

This commit is contained in:
davidsemakula 2024-06-21 17:50:14 +03:00
parent 67f7eb505e
commit d6d45a23d3
2 changed files with 37 additions and 0 deletions

View file

@ -27,6 +27,14 @@ impl Expr {
}
fn needs_parens_in_expr(&self, parent: &Expr) -> bool {
// Parentheses are necessary when calling a function-like pointer that is a member of a struct or union
// (e.g. `(a.f)()`).
let is_parent_call_expr = matches!(parent, ast::Expr::CallExpr(_));
let is_field_expr = matches!(self, ast::Expr::FieldExpr(_));
if is_parent_call_expr && is_field_expr {
return true;
}
// Special-case block weirdness
if parent.child_is_followed_by_a_block() {
use Expr::*;