Fix more cases of when in guard

This commit is contained in:
Joshua Warner 2025-01-11 10:06:05 -08:00
parent 903b792fd3
commit 6b08f42d02
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
5 changed files with 99 additions and 0 deletions

View file

@ -1779,6 +1779,8 @@ fn guard_needs_parens(value: &Expr<'_>) -> bool {
Expr::ParensAround(expr) | Expr::SpaceBefore(expr, _) | Expr::SpaceAfter(expr, _) => {
guard_needs_parens(expr)
}
Expr::Closure(_, body) => guard_needs_parens(&body.value),
Expr::Defs(_, final_expr) => guard_needs_parens(&final_expr.value),
_ => false,
}
}

View file

@ -0,0 +1,10 @@
when
f
is
s if (\t ->
when
0
is
z ->
f
z) -> m

View file

@ -0,0 +1,81 @@
@0-38 SpaceAfter(
When(
@5-6 SpaceAfter(
Var {
module_name: "",
ident: "f",
},
[
Newline,
],
),
[
WhenBranch {
patterns: [
@10-11 SpaceBefore(
Identifier {
ident: "s",
},
[
Newline,
],
),
],
value: @37-38 Var {
module_name: "",
ident: "m",
},
guard: Some(
@14-35 Closure(
[
@15-16 Identifier {
ident: "t",
},
],
@18-35 When(
@23-24 SpaceAfter(
Num(
"0",
),
[
Newline,
],
),
[
WhenBranch {
patterns: [
@28-29 Identifier {
ident: "z",
},
],
value: @31-35 Apply(
@31-32 Var {
module_name: "",
ident: "f",
},
[
@34-35 SpaceBefore(
Var {
module_name: "",
ident: "z",
},
[
Newline,
],
),
],
Space,
),
guard: None,
},
],
),
),
),
},
],
),
[
Newline,
],
)

View file

@ -0,0 +1,5 @@
when f
is
s if\t->when 0
is z->f
z->m

View file

@ -759,6 +759,7 @@ mod test_snapshots {
pass/when_if_guard.expr,
pass/when_in_assignment.expr,
pass/when_in_binops.expr,
pass/when_in_closure_in_when_guard_wtf.expr,
pass/when_in_function.expr,
pass/when_in_function_python_style_indent.expr,
pass/when_in_list.expr,