Skip to content

Commit 82c1e61

Browse files
committed
Fix assists assuming comma belonging to MATCH_ARM_LIST
1 parent f04cff1 commit 82c1e61

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

crates/ide_assists/src/handlers/merge_match_arms.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option
7272
.join(" | ")
7373
};
7474

75-
let arm = format!("{} => {}", pats, current_expr.syntax().text());
75+
let arm = format!("{} => {},", pats, current_expr.syntax().text());
7676

7777
if let [first, .., last] = &*arms_to_merge {
7878
let start = first.syntax().text_range().start();
@@ -118,7 +118,7 @@ enum X { A, B, C }
118118
fn main() {
119119
let x = X::A;
120120
let y = match x {
121-
X::A | X::B => { 1i32 }
121+
X::A | X::B => { 1i32 },
122122
X::C => { 2i32 }
123123
}
124124
}
@@ -183,7 +183,7 @@ fn main() {
183183
let x = X::A;
184184
let y = match x {
185185
X::A => { 1i32 },
186-
_ => { 2i32 }
186+
_ => { 2i32 },
187187
}
188188
}
189189
"#,

crates/ide_diagnostics/src/handlers/inactive_code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn f() {
6464
match () {
6565
() => (),
6666
#[cfg(a)] () => (),
67-
//^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: a is disabled
67+
//^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: a is disabled
6868
}
6969
7070
#[cfg(a)] 0 // Trailing expression of block

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,10 @@ impl ast::MatchArmList {
359359
let mut elements = Vec::new();
360360
let position = match self.arms().last() {
361361
Some(last_arm) => {
362-
let comma = last_arm
363-
.syntax()
364-
.siblings_with_tokens(Direction::Next)
365-
.find(|it| it.kind() == T![,]);
366-
if needs_comma(&last_arm) && comma.is_none() {
367-
elements.push(make::token(SyntaxKind::COMMA).into());
362+
if needs_comma(&last_arm) {
363+
ted::append_child(last_arm.syntax(), make::token(SyntaxKind::COMMA));
368364
}
369-
Position::after(comma.unwrap_or_else(|| last_arm.syntax().clone().into()))
365+
Position::after(last_arm.syntax().clone())
370366
}
371367
None => match self.l_curly_token() {
372368
Some(it) => Position::after(it),
@@ -377,12 +373,12 @@ impl ast::MatchArmList {
377373
elements.push(make::tokens::whitespace(&format!("\n{}", indent)).into());
378374
elements.push(arm.syntax().clone().into());
379375
if needs_comma(&arm) {
380-
elements.push(make::token(SyntaxKind::COMMA).into());
376+
ted::append_child(arm.syntax(), make::token(SyntaxKind::COMMA));
381377
}
382378
ted::insert_all(position, elements);
383379

384380
fn needs_comma(arm: &ast::MatchArm) -> bool {
385-
arm.expr().map_or(false, |e| !e.is_block_like())
381+
arm.expr().map_or(false, |e| !e.is_block_like()) && arm.comma_token().is_none()
386382
}
387383
}
388384
}

0 commit comments

Comments
 (0)