Skip to content

Commit 5983d37

Browse files
bors[bot]Veykril
andauthored
Merge #9691
9691: fix: Keep catch-all arm in fill_match_arms if it has a non-empty expression r=Veykril a=Veykril Fixes #4165 bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 2fe586a + 0a13259 commit 5983d37

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

crates/ide_assists/src/handlers/fill_match_arms.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,18 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
136136
.arms()
137137
.find(|arm| matches!(arm.pat(), Some(ast::Pat::WildcardPat(_))));
138138
if let Some(arm) = catch_all_arm {
139-
arm.remove();
139+
let is_empty_expr = arm.expr().map_or(true, |e| match e {
140+
ast::Expr::BlockExpr(b) => {
141+
b.statements().next().is_none() && b.tail_expr().is_none()
142+
}
143+
ast::Expr::TupleExpr(t) => t.fields().next().is_none(),
144+
_ => false,
145+
});
146+
if is_empty_expr {
147+
arm.remove();
148+
} else {
149+
cov_mark::hit!(fill_match_arms_empty_expr);
150+
}
140151
}
141152
let mut first_new_arm = None;
142153
for arm in missing_arms {
@@ -1093,6 +1104,28 @@ fn foo(t: bool) {
10931104
true => 1 + 2,
10941105
$0false => todo!(),
10951106
}
1107+
}"#,
1108+
);
1109+
}
1110+
1111+
#[test]
1112+
fn does_not_remove_catch_all_with_non_empty_expr() {
1113+
cov_mark::check!(fill_match_arms_empty_expr);
1114+
check_assist(
1115+
fill_match_arms,
1116+
r#"
1117+
fn foo(t: bool) {
1118+
match $0t {
1119+
_ => 1 + 2,
1120+
}
1121+
}"#,
1122+
r#"
1123+
fn foo(t: bool) {
1124+
match t {
1125+
_ => 1 + 2,
1126+
$0true => todo!(),
1127+
false => todo!(),
1128+
}
10961129
}"#,
10971130
);
10981131
}

0 commit comments

Comments
 (0)