Disallow eliding empty record destructure assignment if expr looks like an implements clause (fixes #7366)

This commit is contained in:
Joshua Warner 2024-12-13 20:19:18 -08:00
parent f86f440f2f
commit c54b01016e
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
7 changed files with 91 additions and 3 deletions

View file

@ -1049,6 +1049,7 @@ 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
};
@ -1172,6 +1173,34 @@ 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 {
match expr.extract_spaces().item {
Expr::Var { module_name, ident } => {
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),