Skip to content

Commit 4095cfa

Browse files
authored
Merge pull request #21904 from A4-Tacks/no-panic-unmerge-trailing
fix: don't panic unmerge arm on trailing pipe
2 parents 912571e + afe522a commit 4095cfa

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

src/tools/rust-analyzer/crates/ide-assists/src/handlers/unmerge_match_arm.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@ pub(crate) fn unmerge_match_arm(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
3838
}
3939
let match_arm = ast::MatchArm::cast(or_pat.syntax().parent()?)?;
4040
let match_arm_body = match_arm.expr()?;
41+
let pats_after = pipe_token
42+
.siblings_with_tokens(Direction::Next)
43+
.filter_map(|it| ast::Pat::cast(it.into_node()?))
44+
.collect::<Vec<_>>();
4145

4246
// We don't need to check for leading pipe because it is directly under `MatchArm`
4347
// without `OrPat`.
4448

4549
let new_parent = match_arm.syntax().parent()?;
50+
if pats_after.is_empty() {
51+
return None;
52+
}
4653

4754
acc.add(
4855
AssistId::refactor_rewrite("unmerge_match_arm"),
@@ -51,10 +58,6 @@ pub(crate) fn unmerge_match_arm(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
5158
|edit| {
5259
let make = SyntaxFactory::with_mappings();
5360
let mut editor = edit.make_editor(&new_parent);
54-
let pats_after = pipe_token
55-
.siblings_with_tokens(Direction::Next)
56-
.filter_map(|it| ast::Pat::cast(it.into_node()?))
57-
.collect::<Vec<_>>();
5861
// It is guaranteed that `pats_after` has at least one element
5962
let new_pat = if pats_after.len() == 1 {
6063
pats_after[0].clone()
@@ -190,6 +193,21 @@ fn main() {
190193
);
191194
}
192195

196+
#[test]
197+
fn unmerge_match_arm_trailing_pipe() {
198+
check_assist_not_applicable(
199+
unmerge_match_arm,
200+
r#"
201+
fn main() {
202+
let y = match 0 {
203+
0 |$0 => { 1i32 }
204+
1 => { 2i32 }
205+
};
206+
}
207+
"#,
208+
);
209+
}
210+
193211
#[test]
194212
fn unmerge_match_arm_multiple_pipes() {
195213
check_assist(

0 commit comments

Comments
 (0)