fmt: Add parens around as patterns in arguments

This commit is contained in:
Agus Zubiaga 2024-09-02 11:54:34 -03:00
parent 2679ba3e95
commit 2486ce0e48
No known key found for this signature in database
5 changed files with 105 additions and 0 deletions

View file

@ -1240,8 +1240,51 @@ fn fmt_closure<'a>(
let mut it = loc_patterns.iter().peekable();
while let Some(loc_pattern) = it.next() {
let requires_parens = {
let mut curr = loc_pattern.value;
loop {
match curr {
Pattern::SpaceAfter(pattern, _) | Pattern::SpaceBefore(pattern, _) => {
curr = *pattern;
}
Pattern::As(_, _) => {
break true;
}
Pattern::Identifier { .. }
| Pattern::QualifiedIdentifier { .. }
| Pattern::Tag(_)
| Pattern::OpaqueRef(_)
| Pattern::Apply(_, _)
| Pattern::RecordDestructure(_)
| Pattern::Tuple(_)
| Pattern::Underscore(_)
| Pattern::RequiredField(_, _)
| Pattern::OptionalField(_, _)
| Pattern::NumLiteral(_)
| Pattern::NonBase10Literal { .. }
| Pattern::FloatLiteral(_)
| Pattern::StrLiteral(_)
| Pattern::SingleQuote(_)
| Pattern::List(_)
| Pattern::ListRest(_)
| Pattern::Malformed(_)
| Pattern::MalformedIdent(_, _) => {
break false;
}
}
}
};
if requires_parens {
buf.push('(');
}
loc_pattern.format(buf, indent);
if requires_parens {
buf.push(')');
}
if it.peek().is_some() {
buf.indent(indent);
if arguments_are_multiline {