Add parens around implements identifier to be conservative

This commit is contained in:
Joshua Warner 2024-12-14 15:12:46 -08:00
parent 24dd11262e
commit 24ba9dbeba
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
10 changed files with 140 additions and 36 deletions

View file

@ -1056,7 +1056,6 @@ pub fn fmt_body<'a>(
&& !matches!(body.extract_spaces().item, Expr::Defs(..))
&& !matches!(body.extract_spaces().item, Expr::Return(..))
&& !starts_with_expect_ident(body)
&& !might_be_confused_with_implements(body)
} else {
false
};
@ -1180,29 +1179,6 @@ fn starts_with_expect_ident(expr: &Expr<'_>) -> bool {
}
}
fn might_be_confused_with_implements(body: &Expr<'_>) -> bool {
// As with `expect`, we need to be careful about things that might "become" `implements` clauses
// if parsed at a statement level.
match body {
Expr::Apply(func, args, _) => {
if !matches!(func.extract_spaces().item, Expr::Tag(..)) {
return false;
}
for expr in *args {
if let Expr::Var { module_name, ident } = expr.extract_spaces().item {
if module_name.is_empty() && (ident == "implements" || ident == "implements!") {
return true;
}
}
}
false
}
_ => false,
}
}
pub fn starts_with_block_string_literal(expr: &Expr<'_>) -> bool {
match expr {
Expr::Str(s) => is_str_multiline(s),

View file

@ -715,20 +715,16 @@ fn fmt_apply(
}
last_after = arg.after;
if should_reflow_outdentable {
buf.spaces(1);
// Ignore any comments+newlines before/after.
// We checked above that there's only a single newline before the last arg,
// which we're intentionally ignoring.
format_expr_only(&arg.item, buf, Parens::InApply, Newlines::Yes, arg_indent);
} else if needs_indent {
if needs_indent {
buf.ensure_ends_with_newline();
format_expr_only(&arg.item, buf, Parens::InApply, Newlines::Yes, arg_indent);
} else {
buf.spaces(1);
}
if matches!(arg.item, Expr::Var { module_name, ident } if module_name.is_empty() && ident == "implements")
{
fmt_parens(&arg.item, buf, arg_indent);
} else {
format_expr_only(&arg.item, buf, Parens::InApply, Newlines::Yes, arg_indent);
}
}