From 071ac48b6cdb5e4dfdf797f5d14b3173b8e78005 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 2 Jul 2021 01:44:54 +0200 Subject: [PATCH] Remove make::match_arm_with_guard --- .../ide_assists/src/handlers/early_return.rs | 3 ++- .../src/handlers/extract_function.rs | 12 ++++++++---- .../src/handlers/fill_match_arms.rs | 2 +- .../src/handlers/replace_if_let_with_match.rs | 18 +++++++++++------- .../src/handlers/replace_unwrap_with_match.rs | 3 ++- crates/syntax/src/ast/edit.rs | 2 +- crates/syntax/src/ast/make.rs | 11 +++++++++-- 7 files changed, 34 insertions(+), 17 deletions(-) diff --git a/crates/ide_assists/src/handlers/early_return.rs b/crates/ide_assists/src/handlers/early_return.rs index ef4a7cb50c8..174c2e29540 100644 --- a/crates/ide_assists/src/handlers/early_return.rs +++ b/crates/ide_assists/src/handlers/early_return.rs @@ -133,12 +133,13 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) let path = make::ext::ident_path("it"); 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( // FIXME: would be cool to use `None` or `Err(_)` if appropriate once(make::wildcard_pat().into()), + None, early_expression, ); diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index 870d4f66563..97df906f829 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs @@ -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 pat = make::tuple_struct_pat(path, iter::once(value_pat.into())); 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 path = make::ext::ident_path("None"); 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]); 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 pat = make::tuple_struct_pat(path, iter::once(value_pat.into())); 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 path = make::ext::ident_path("Err"); let value_pat = make::ident_pat(make::name(err_name)); let pat = make::tuple_struct_pat(path, iter::once(value_pat.into())); 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]); make::expr_match(call_expr, arms) diff --git a/crates/ide_assists/src/handlers/fill_match_arms.rs b/crates/ide_assists/src/handlers/fill_match_arms.rs index 318faa0fca6..4f6d68e45a5 100644 --- a/crates/ide_assists/src/handlers/fill_match_arms.rs +++ b/crates/ide_assists/src/handlers/fill_match_arms.rs @@ -129,7 +129,7 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option< |builder| { let new_match_arm_list = match_arm_list.clone_for_update(); 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()); let catch_all_arm = new_match_arm_list diff --git a/crates/ide_assists/src/handlers/replace_if_let_with_match.rs b/crates/ide_assists/src/handlers/replace_if_let_with_match.rs index 6851cf83817..4934bb41870 100644 --- a/crates/ide_assists/src/handlers/replace_if_let_with_match.rs +++ b/crates/ide_assists/src/handlers/replace_if_let_with_match.rs @@ -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() { // Only if all condition expressions are equal we can merge them into a match return None; - } else { - pat_seen = true; - Either::Left(pat) } + pat_seen = true; + Either::Left(pat) } None => Either::Right(expr), }; @@ -105,10 +104,15 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext) _ => None, } .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( iter::once(make::wildcard_pat().into()), + None, 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)); match 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()), - expr, + Some(expr), unwrap_trivial_block(body), ), } diff --git a/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs index f39c48d8f7b..0e41aeb89d3 100644 --- a/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs @@ -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 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( iter::once(make::wildcard_pat().into()), + None, make::ext::expr_unreachable(), ); diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index 2663c0759d2..ff31bf5cc73 100644 --- a/crates/syntax/src/ast/edit.rs +++ b/crates/syntax/src/ast/edit.rs @@ -228,7 +228,7 @@ fn single_node(element: impl Into) -> RangeInclusive ast::Pat { } } -pub fn match_arm(pats: impl IntoIterator, expr: ast::Expr) -> ast::MatchArm { +pub fn match_arm( + pats: impl IntoIterator, + guard: Option, + expr: ast::Expr, +) -> ast::MatchArm { 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 { ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text))