1 use std::iter::{self, successors};
4 use ide_db::{ty_filter::TryEnum, RootDatabase};
8 edit::{AstNodeEdit, IndentLevel},
15 utils::{does_pat_match_variant, unwrap_trivial_block},
16 AssistContext, AssistId, AssistKind, Assists,
19 // Assist: replace_if_let_with_match
21 // Replaces a `if let` expression with a `match` expression.
24 // enum Action { Move { distance: u32 }, Stop }
26 // fn handle(action: Action) {
27 // $0if let Action::Move { distance } = action {
36 // enum Action { Move { distance: u32 }, Stop }
38 // fn handle(action: Action) {
40 // Action::Move { distance } => foo(distance),
45 pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
46 let if_expr: ast::IfExpr = ctx.find_node_at_offset()?;
47 let mut else_block = None;
48 let if_exprs = successors(Some(if_expr.clone()), |expr| match expr.else_branch()? {
49 ast::ElseBranch::IfExpr(expr) => Some(expr),
50 ast::ElseBranch::Block(block) => {
51 else_block = Some(block);
55 let scrutinee_to_be_expr = if_expr.condition()?.expr()?;
57 let mut pat_seen = false;
58 let mut cond_bodies = Vec::new();
59 for if_expr in if_exprs {
60 let cond = if_expr.condition()?;
61 let expr = cond.expr()?;
62 let cond = match cond.pat() {
64 if scrutinee_to_be_expr.syntax().text() != expr.syntax().text() {
65 // Only if all condition expressions are equal we can merge them into a match
71 None => Either::Right(expr),
73 let body = if_expr.then_branch()?;
74 cond_bodies.push((cond, body));
78 // Don't offer turning an if (chain) without patterns into a match
82 let target = if_expr.syntax().text_range();
84 AssistId("replace_if_let_with_match", AssistKind::RefactorRewrite),
85 "Replace if let with match",
89 let else_arm = make_else_arm(ctx, else_block, &cond_bodies);
90 let make_match_arm = |(pat, body): (_, ast::BlockExpr)| {
91 let body = body.reset_indent().indent(IndentLevel(1));
93 Either::Left(pat) => {
94 make::match_arm(iter::once(pat), None, unwrap_trivial_block(body))
96 Either::Right(expr) => make::match_arm(
97 iter::once(make::wildcard_pat().into()),
99 unwrap_trivial_block(body),
103 let arms = cond_bodies.into_iter().map(make_match_arm).chain(iter::once(else_arm));
104 let match_expr = make::expr_match(scrutinee_to_be_expr, make::match_arm_list(arms));
105 match_expr.indent(IndentLevel::from_node(if_expr.syntax()))
108 let has_preceding_if_expr =
109 if_expr.syntax().parent().map_or(false, |it| ast::IfExpr::can_cast(it.kind()));
110 let expr = if has_preceding_if_expr {
111 // make sure we replace the `else if let ...` with a block so we don't end up with `else expr`
112 make::block_expr(None, Some(match_expr)).into()
116 edit.replace_ast::<ast::Expr>(if_expr.into(), expr);
123 else_block: Option<ast::BlockExpr>,
124 conditionals: &[(Either<ast::Pat, ast::Expr>, ast::BlockExpr)],
126 if let Some(else_block) = else_block {
127 let pattern = if let [(Either::Left(pat), _)] = conditionals {
130 .and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty))
135 let pattern = match pattern {
137 if does_pat_match_variant(&pat, &it.sad_pattern()) {
143 None => make::wildcard_pat().into(),
145 make::match_arm(iter::once(pattern), None, unwrap_trivial_block(else_block))
147 make::match_arm(iter::once(make::wildcard_pat().into()), None, make::expr_unit().into())
151 // Assist: replace_match_with_if_let
153 // Replaces a binary `match` with a wildcard pattern and no guards with an `if let` expression.
156 // enum Action { Move { distance: u32 }, Stop }
158 // fn handle(action: Action) {
160 // Action::Move { distance } => foo(distance),
167 // enum Action { Move { distance: u32 }, Stop }
169 // fn handle(action: Action) {
170 // if let Action::Move { distance } = action {
177 pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
178 let match_expr: ast::MatchExpr = ctx.find_node_at_offset()?;
180 let mut arms = match_expr.match_arm_list()?.arms();
181 let (first_arm, second_arm) = (arms.next()?, arms.next()?);
182 if arms.next().is_some() || first_arm.guard().is_some() || second_arm.guard().is_some() {
186 let (if_let_pat, then_expr, else_expr) = pick_pattern_and_expr_order(
193 let scrutinee = match_expr.expr()?;
195 let target = match_expr.syntax().text_range();
197 AssistId("replace_match_with_if_let", AssistKind::RefactorRewrite),
198 "Replace match with if let",
201 let condition = make::condition(scrutinee, Some(if_let_pat));
202 let then_block = match then_expr.reset_indent() {
203 ast::Expr::BlockExpr(block) => block,
204 expr => make::block_expr(iter::empty(), Some(expr)),
206 let else_expr = match else_expr {
207 ast::Expr::BlockExpr(block) if block.is_empty() => None,
208 ast::Expr::TupleExpr(tuple) if tuple.fields().next().is_none() => None,
211 let if_let_expr = make::expr_if(
215 .map(|expr| match expr {
216 ast::Expr::BlockExpr(block) => block,
217 expr => (make::block_expr(iter::empty(), Some(expr))),
219 .map(ast::ElseBranch::Block),
221 .indent(IndentLevel::from_node(match_expr.syntax()));
223 edit.replace_ast::<ast::Expr>(match_expr.into(), if_let_expr);
228 /// Pick the pattern for the if let condition and return the expressions for the `then` body and `else` body in that order.
229 fn pick_pattern_and_expr_order(
230 sema: &hir::Semantics<RootDatabase>,
235 ) -> Option<(ast::Pat, ast::Expr, ast::Expr)> {
236 let res = match (pat, pat2) {
237 (ast::Pat::WildcardPat(_), _) => return None,
238 (pat, sad_pat) if is_sad_pat(sema, &sad_pat) => (pat, expr, expr2),
239 (sad_pat, pat) if is_sad_pat(sema, &sad_pat) => (pat, expr2, expr),
240 (pat, pat2) => match (binds_name(&pat), binds_name(&pat2)) {
241 (true, true) => return None,
242 (true, false) => (pat, expr, expr2),
243 (false, true) => (pat2, expr2, expr),
244 (false, false) => (pat, expr, expr2),
250 fn binds_name(pat: &ast::Pat) -> bool {
251 let binds_name_v = |pat| binds_name(&pat);
253 ast::Pat::IdentPat(_) => true,
254 ast::Pat::MacroPat(_) => true,
255 ast::Pat::OrPat(pat) => pat.pats().any(binds_name_v),
256 ast::Pat::SlicePat(pat) => pat.pats().any(binds_name_v),
257 ast::Pat::TuplePat(it) => it.fields().any(binds_name_v),
258 ast::Pat::TupleStructPat(it) => it.fields().any(binds_name_v),
259 ast::Pat::RecordPat(it) => it
260 .record_pat_field_list()
261 .map_or(false, |rpfl| rpfl.fields().flat_map(|rpf| rpf.pat()).any(binds_name_v)),
262 ast::Pat::RefPat(pat) => pat.pat().map_or(false, binds_name_v),
263 ast::Pat::BoxPat(pat) => pat.pat().map_or(false, binds_name_v),
264 ast::Pat::ParenPat(pat) => pat.pat().map_or(false, binds_name_v),
269 fn is_sad_pat(sema: &hir::Semantics<RootDatabase>, pat: &ast::Pat) -> bool {
270 sema.type_of_pat(pat)
271 .and_then(|ty| TryEnum::from_ty(sema, &ty))
272 .map_or(false, |it| does_pat_match_variant(pat, &it.sad_pattern()))
279 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
282 fn test_if_let_with_match_unapplicable_for_simple_ifs() {
283 check_assist_not_applicable(
284 replace_if_let_with_match,
287 if $0true {} else if false {} else {}
294 fn test_if_let_with_match_no_else() {
296 replace_if_let_with_match,
300 if $0let VariantData::Struct(..) = *self {
310 VariantData::Struct(..) => {
322 fn test_if_let_with_match_basic() {
324 replace_if_let_with_match,
327 pub fn is_struct(&self) -> bool {
328 if $0let VariantData::Struct(..) = *self {
330 } else if let VariantData::Tuple(..) = *self {
344 pub fn is_struct(&self) -> bool {
346 VariantData::Struct(..) => true,
347 VariantData::Tuple(..) => false,
362 fn test_if_let_with_match_on_tail_if_let() {
364 replace_if_let_with_match,
367 pub fn is_struct(&self) -> bool {
368 if let VariantData::Struct(..) = *self {
370 } else if let$0 VariantData::Tuple(..) = *self {
380 pub fn is_struct(&self) -> bool {
381 if let VariantData::Struct(..) = *self {
385 VariantData::Tuple(..) => false,
396 fn special_case_option() {
398 replace_if_let_with_match,
401 fn foo(x: Option<i32>) {
402 $0if let Some(x) = x {
410 fn foo(x: Option<i32>) {
412 Some(x) => println!("{}", x),
413 None => println!("none"),
421 fn special_case_inverted_option() {
423 replace_if_let_with_match,
426 fn foo(x: Option<i32>) {
435 fn foo(x: Option<i32>) {
437 None => println!("none"),
438 Some(_) => println!("some"),
446 fn special_case_result() {
448 replace_if_let_with_match,
451 fn foo(x: Result<i32, ()>) {
460 fn foo(x: Result<i32, ()>) {
462 Ok(x) => println!("{}", x),
463 Err(_) => println!("none"),
471 fn special_case_inverted_result() {
473 replace_if_let_with_match,
476 fn foo(x: Result<i32, ()>) {
477 $0if let Err(x) = x {
485 fn foo(x: Result<i32, ()>) {
487 Err(x) => println!("{}", x),
488 Ok(_) => println!("ok"),
498 replace_if_let_with_match,
502 $0if let Ok(rel_path) = path.strip_prefix(root_path) {
503 let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
504 Some((*id, rel_path))
514 match path.strip_prefix(root_path) {
516 let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
517 Some((*id, rel_path))
528 fn test_replace_match_with_if_let_unwraps_simple_expressions() {
530 replace_match_with_if_let,
533 pub fn is_struct(&self) -> bool {
535 VariantData::Struct(..) => true,
542 pub fn is_struct(&self) -> bool {
543 if let VariantData::Struct(..) = *self {
554 fn test_replace_match_with_if_let_doesnt_unwrap_multiline_expressions() {
556 replace_match_with_if_let,
560 VariantData::Struct(..) => {
570 if let VariantData::Struct(..) = a {
582 fn replace_match_with_if_let_target() {
584 replace_match_with_if_let,
587 pub fn is_struct(&self) -> bool {
589 VariantData::Struct(..) => true,
595 VariantData::Struct(..) => true,
602 fn special_case_option_match_to_if_let() {
604 replace_match_with_if_let,
607 fn foo(x: Option<i32>) {
609 Some(x) => println!("{}", x),
610 None => println!("none"),
615 fn foo(x: Option<i32>) {
627 fn special_case_result_match_to_if_let() {
629 replace_match_with_if_let,
632 fn foo(x: Result<i32, ()>) {
634 Ok(x) => println!("{}", x),
635 Err(_) => println!("none"),
640 fn foo(x: Result<i32, ()>) {
652 fn nested_indent_match_to_if_let() {
654 replace_match_with_if_let,
658 $0match path.strip_prefix(root_path) {
660 let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
661 Some((*id, rel_path))
671 if let Ok(rel_path) = path.strip_prefix(root_path) {
672 let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
673 Some((*id, rel_path))
684 fn replace_match_with_if_let_empty_wildcard_expr() {
686 replace_match_with_if_let,
689 $0match path.strip_prefix(root_path) {
690 Ok(rel_path) => println!("{}", rel_path),
697 if let Ok(rel_path) = path.strip_prefix(root_path) {
698 println!("{}", rel_path)
706 fn replace_match_with_if_let_exhaustive() {
708 replace_match_with_if_let,
710 fn print_source(def_source: ModuleSource) {
712 ModuleSource::SourceFile(..) => { println!("source file"); }
713 ModuleSource::Module(..) => { println!("module"); }
718 fn print_source(def_source: ModuleSource) {
719 if let ModuleSource::SourceFile(..) = def_source { println!("source file"); } else { println!("module"); }
726 fn replace_match_with_if_let_prefer_name_bind() {
728 replace_match_with_if_let,
733 Bar(bar) => println!("bar {}", bar),
739 if let Bar(bar) = Foo(0) {
740 println!("bar {}", bar)
746 replace_match_with_if_let,
750 Bar(bar) => println!("bar {}", bar),
757 if let Bar(bar) = Foo(0) {
758 println!("bar {}", bar)
766 fn replace_match_with_if_let_rejects_double_name_bindings() {
767 check_assist_not_applicable(
768 replace_match_with_if_let,
772 Foo(foo) => println!("bar {}", foo),
773 Bar(bar) => println!("bar {}", bar),