minor: Handle match arm commas in make::match_arm

Co-authored-by: Giga Bowser <45986823+Giga-Bowser@users.noreply.github.com>
This commit is contained in:
Lukas Wirth 2025-07-04 11:05:05 +02:00
parent 4183bcdcde
commit 2c01609d6d
3 changed files with 11 additions and 21 deletions

View file

@ -842,9 +842,10 @@ pub fn ref_pat(pat: ast::Pat) -> ast::RefPat {
}
pub fn match_arm(pat: ast::Pat, guard: Option<ast::MatchGuard>, expr: ast::Expr) -> ast::MatchArm {
let comma_str = if expr.is_block_like() { "" } else { "," };
return match guard {
Some(guard) => from_text(&format!("{pat} {guard} => {expr}")),
None => from_text(&format!("{pat} => {expr}")),
Some(guard) => from_text(&format!("{pat} {guard} => {expr}{comma_str}")),
None => from_text(&format!("{pat} => {expr}{comma_str}")),
};
fn from_text(text: &str) -> ast::MatchArm {
@ -877,7 +878,7 @@ pub fn match_arm_list(arms: impl IntoIterator<Item = ast::MatchArm>) -> ast::Mat
let arms_str = arms.into_iter().fold(String::new(), |mut acc, arm| {
let needs_comma =
arm.comma_token().is_none() && arm.expr().is_none_or(|it| !it.is_block_like());
let comma = if needs_comma { "," } else { "" };
let comma = if needs_comma && arm.comma_token().is_none() { "," } else { "" };
let arm = arm.syntax();
format_to_acc!(acc, " {arm}{comma}\n")
});

View file

@ -435,7 +435,7 @@ mod tests {
_ => {
let var_name = 2 + 2;
(var_name, true)
}"#]];
},"#]];
expect.assert_eq(&edit.new_root.to_string());
assert_eq!(edit.find_annotation(placeholder_snippet).len(), 2);