mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-16 17:58:16 +00:00
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 <lukastw97@gmail.com>
This commit is contained in:
commit
5983d3745a
1 changed files with 34 additions and 1 deletions
|
@ -136,7 +136,18 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
|
|||
.arms()
|
||||
.find(|arm| matches!(arm.pat(), Some(ast::Pat::WildcardPat(_))));
|
||||
if let Some(arm) = catch_all_arm {
|
||||
arm.remove();
|
||||
let is_empty_expr = arm.expr().map_or(true, |e| match e {
|
||||
ast::Expr::BlockExpr(b) => {
|
||||
b.statements().next().is_none() && b.tail_expr().is_none()
|
||||
}
|
||||
ast::Expr::TupleExpr(t) => t.fields().next().is_none(),
|
||||
_ => false,
|
||||
});
|
||||
if is_empty_expr {
|
||||
arm.remove();
|
||||
} else {
|
||||
cov_mark::hit!(fill_match_arms_empty_expr);
|
||||
}
|
||||
}
|
||||
let mut first_new_arm = None;
|
||||
for arm in missing_arms {
|
||||
|
@ -1093,6 +1104,28 @@ fn foo(t: bool) {
|
|||
true => 1 + 2,
|
||||
$0false => todo!(),
|
||||
}
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn does_not_remove_catch_all_with_non_empty_expr() {
|
||||
cov_mark::check!(fill_match_arms_empty_expr);
|
||||
check_assist(
|
||||
fill_match_arms,
|
||||
r#"
|
||||
fn foo(t: bool) {
|
||||
match $0t {
|
||||
_ => 1 + 2,
|
||||
}
|
||||
}"#,
|
||||
r#"
|
||||
fn foo(t: bool) {
|
||||
match t {
|
||||
_ => 1 + 2,
|
||||
$0true => todo!(),
|
||||
false => todo!(),
|
||||
}
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue