Remove make::match_arm_with_guard

This commit is contained in:
Lukas Wirth 2021-07-02 01:44:54 +02:00
parent 8967856d78
commit 071ac48b6c
7 changed files with 34 additions and 17 deletions

View File

@ -133,12 +133,13 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
let path = make::ext::ident_path("it"); let path = make::ext::ident_path("it");
make::expr_path(path) make::expr_path(path)
}; };
make::match_arm(once(pat.into()), expr) make::match_arm(once(pat.into()), None, expr)
}; };
let sad_arm = make::match_arm( let sad_arm = make::match_arm(
// FIXME: would be cool to use `None` or `Err(_)` if appropriate // FIXME: would be cool to use `None` or `Err(_)` if appropriate
once(make::wildcard_pat().into()), once(make::wildcard_pat().into()),
None,
early_expression, early_expression,
); );

View File

@ -1089,12 +1089,12 @@ fn make_call_expr(&self, call_expr: ast::Expr) -> ast::Expr {
let value_pat = make::ident_pat(make::name(some_name)); let value_pat = make::ident_pat(make::name(some_name));
let pat = make::tuple_struct_pat(path, iter::once(value_pat.into())); let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
let value = make::expr_path(make::ext::ident_path(some_name)); let value = make::expr_path(make::ext::ident_path(some_name));
make::match_arm(iter::once(pat.into()), value) make::match_arm(iter::once(pat.into()), None, value)
}; };
let none_arm = { let none_arm = {
let path = make::ext::ident_path("None"); let path = make::ext::ident_path("None");
let pat = make::path_pat(path); let pat = make::path_pat(path);
make::match_arm(iter::once(pat), none.make_result_handler(None)) make::match_arm(iter::once(pat), None, none.make_result_handler(None))
}; };
let arms = make::match_arm_list(vec![some_arm, none_arm]); let arms = make::match_arm_list(vec![some_arm, none_arm]);
make::expr_match(call_expr, arms) make::expr_match(call_expr, arms)
@ -1108,14 +1108,18 @@ fn make_call_expr(&self, call_expr: ast::Expr) -> ast::Expr {
let value_pat = make::ident_pat(make::name(ok_name)); let value_pat = make::ident_pat(make::name(ok_name));
let pat = make::tuple_struct_pat(path, iter::once(value_pat.into())); let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
let value = make::expr_path(make::ext::ident_path(ok_name)); let value = make::expr_path(make::ext::ident_path(ok_name));
make::match_arm(iter::once(pat.into()), value) make::match_arm(iter::once(pat.into()), None, value)
}; };
let err_arm = { let err_arm = {
let path = make::ext::ident_path("Err"); let path = make::ext::ident_path("Err");
let value_pat = make::ident_pat(make::name(err_name)); let value_pat = make::ident_pat(make::name(err_name));
let pat = make::tuple_struct_pat(path, iter::once(value_pat.into())); let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
let value = make::expr_path(make::ext::ident_path(err_name)); let value = make::expr_path(make::ext::ident_path(err_name));
make::match_arm(iter::once(pat.into()), err.make_result_handler(Some(value))) make::match_arm(
iter::once(pat.into()),
None,
err.make_result_handler(Some(value)),
)
}; };
let arms = make::match_arm_list(vec![ok_arm, err_arm]); let arms = make::match_arm_list(vec![ok_arm, err_arm]);
make::expr_match(call_expr, arms) make::expr_match(call_expr, arms)

View File

@ -129,7 +129,7 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
|builder| { |builder| {
let new_match_arm_list = match_arm_list.clone_for_update(); let new_match_arm_list = match_arm_list.clone_for_update();
let missing_arms = missing_pats let missing_arms = missing_pats
.map(|pat| make::match_arm(iter::once(pat), make::ext::expr_todo())) .map(|pat| make::match_arm(iter::once(pat), None, make::ext::expr_todo()))
.map(|it| it.clone_for_update()); .map(|it| it.clone_for_update());
let catch_all_arm = new_match_arm_list let catch_all_arm = new_match_arm_list

View File

@ -64,10 +64,9 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
if scrutinee_to_be_expr.syntax().text() != expr.syntax().text() { if scrutinee_to_be_expr.syntax().text() != expr.syntax().text() {
// Only if all condition expressions are equal we can merge them into a match // Only if all condition expressions are equal we can merge them into a match
return None; return None;
} else {
pat_seen = true;
Either::Left(pat)
} }
pat_seen = true;
Either::Left(pat)
} }
None => Either::Right(expr), None => Either::Right(expr),
}; };
@ -105,10 +104,15 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
_ => None, _ => None,
} }
.unwrap_or_else(|| make::wildcard_pat().into()); .unwrap_or_else(|| make::wildcard_pat().into());
make::match_arm(iter::once(pattern), unwrap_trivial_block(else_block)) make::match_arm(
iter::once(pattern),
None,
unwrap_trivial_block(else_block),
)
} }
None => make::match_arm( None => make::match_arm(
iter::once(make::wildcard_pat().into()), iter::once(make::wildcard_pat().into()),
None,
make::expr_unit().into(), make::expr_unit().into(),
), ),
} }
@ -119,11 +123,11 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
let body = body.reset_indent().indent(IndentLevel(1)); let body = body.reset_indent().indent(IndentLevel(1));
match pat { match pat {
Either::Left(pat) => { Either::Left(pat) => {
make::match_arm(iter::once(pat), unwrap_trivial_block(body)) make::match_arm(iter::once(pat), None, unwrap_trivial_block(body))
} }
Either::Right(expr) => make::match_arm_with_guard( Either::Right(expr) => make::match_arm(
iter::once(make::wildcard_pat().into()), iter::once(make::wildcard_pat().into()),
expr, Some(expr),
unwrap_trivial_block(body), unwrap_trivial_block(body),
), ),
} }

View File

@ -56,10 +56,11 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into();
let bind_path = make::ext::ident_path("it"); let bind_path = make::ext::ident_path("it");
let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); let ok_arm = make::match_arm(iter::once(ok_tuple), None, make::expr_path(bind_path));
let err_arm = make::match_arm( let err_arm = make::match_arm(
iter::once(make::wildcard_pat().into()), iter::once(make::wildcard_pat().into()),
None,
make::ext::expr_unreachable(), make::ext::expr_unreachable(),
); );

View File

@ -228,7 +228,7 @@ fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElemen
#[test] #[test]
fn test_increase_indent() { fn test_increase_indent() {
let arm_list = { let arm_list = {
let arm = make::match_arm(iter::once(make::wildcard_pat().into()), make::expr_unit()); let arm = make::match_arm(iter::once(make::wildcard_pat().into()), None, make::expr_unit());
make::match_arm_list(vec![arm.clone(), arm]) make::match_arm_list(vec![arm.clone(), arm])
}; };
assert_eq!( assert_eq!(

View File

@ -421,9 +421,16 @@ fn from_text(text: &str) -> ast::Pat {
} }
} }
pub fn match_arm(pats: impl IntoIterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm { pub fn match_arm(
pats: impl IntoIterator<Item = ast::Pat>,
guard: Option<ast::Expr>,
expr: ast::Expr,
) -> ast::MatchArm {
let pats_str = pats.into_iter().join(" | "); let pats_str = pats.into_iter().join(" | ");
return from_text(&format!("{} => {}", pats_str, expr)); return match guard {
Some(guard) => from_text(&format!("{} if {} => {}", pats_str, guard, expr)),
None => from_text(&format!("{} => {}", pats_str, expr)),
};
fn from_text(text: &str) -> ast::MatchArm { fn from_text(text: &str) -> ast::MatchArm {
ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text)) ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text))