Don't remove empty record assignment if body looks like an expect

This commit is contained in:
Joshua Warner 2024-12-13 13:56:46 -08:00
parent a1d61c5a3c
commit d82accf83d
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
5 changed files with 70 additions and 0 deletions

View file

@ -1034,6 +1034,7 @@ pub fn fmt_body<'a>(
&& pattern_extracted.after.iter().all(|s| s.is_newline())
&& !matches!(body.extract_spaces().item, Expr::Defs(..))
&& !matches!(body.extract_spaces().item, Expr::Return(..))
&& !starts_with_expect_ident(body)
} else {
false
};
@ -1144,6 +1145,19 @@ pub fn fmt_body<'a>(
}
}
fn starts_with_expect_ident(expr: &Expr<'_>) -> bool {
// We need to be persnickety about not formatting `{}=expect foo` into `expect foo`,
// because `expect` is treated as a keyword at the statement level but not at the expr level.
// If we removed the `{}=` in this case, that would change the meaning
match expr {
Expr::Apply(inner, _, _) => starts_with_expect_ident(&inner.value),
Expr::Var { module_name, ident } => {
module_name.is_empty() && (*ident == "expect" || *ident == "expect!")
}
_ => false,
}
}
pub fn starts_with_block_string_literal(expr: &Expr<'_>) -> bool {
match expr {
Expr::Str(s) => is_str_multiline(s),