aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-08-07 12:51:54 +0100
committerAleksey Kladov <[email protected]>2019-08-07 12:57:24 +0100
commit39967a85e1f04676062d46b04e7ac8399c57df66 (patch)
tree291d68757076b070ade3b4d6e6641dcb643a9b37 /crates
parent4d6475ada0c4176734f7e7f24cb1be8c5c8d1988 (diff)
refactor if-let lowering
mainly to get rid of unwraps
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/expr.rs44
1 files changed, 25 insertions, 19 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 9fea70dda..b59787a83 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -559,6 +559,7 @@ where
559 match expr.kind() { 559 match expr.kind() {
560 ast::ExprKind::IfExpr(e) => { 560 ast::ExprKind::IfExpr(e) => {
561 let then_branch = self.collect_block_opt(e.then_branch()); 561 let then_branch = self.collect_block_opt(e.then_branch());
562
562 let else_branch = e.else_branch().map(|b| match b { 563 let else_branch = e.else_branch().map(|b| match b {
563 ast::ElseBranch::Block(it) => self.collect_block(it), 564 ast::ElseBranch::Block(it) => self.collect_block(it),
564 ast::ElseBranch::IfExpr(elif) => { 565 ast::ElseBranch::IfExpr(elif) => {
@@ -567,25 +568,30 @@ where
567 } 568 }
568 }); 569 });
569 570
570 if let Some(pat) = e.condition().and_then(|c| c.pat()) { 571 let condition = match e.condition() {
571 // if let -- desugar to match 572 None => self.exprs.alloc(Expr::Missing),
572 let pat = self.collect_pat(pat); 573 Some(condition) => match condition.pat() {
573 let match_expr = 574 None => self.collect_expr_opt(condition.expr()),
574 self.collect_expr_opt(e.condition().expect("checked above").expr()); 575 // if let -- desugar to match
575 let placeholder_pat = self.pats.alloc(Pat::Missing); 576 Some(pat) => {
576 let arms = vec![ 577 let pat = self.collect_pat(pat);
577 MatchArm { pats: vec![pat], expr: then_branch, guard: None }, 578 let match_expr = self.collect_expr_opt(condition.expr());
578 MatchArm { 579 let placeholder_pat = self.pats.alloc(Pat::Missing);
579 pats: vec![placeholder_pat], 580 let arms = vec![
580 expr: else_branch.unwrap_or_else(|| self.empty_block()), 581 MatchArm { pats: vec![pat], expr: then_branch, guard: None },
581 guard: None, 582 MatchArm {
582 }, 583 pats: vec![placeholder_pat],
583 ]; 584 expr: else_branch.unwrap_or_else(|| self.empty_block()),
584 self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr) 585 guard: None,
585 } else { 586 },
586 let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr())); 587 ];
587 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) 588 return self
588 } 589 .alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr);
590 }
591 },
592 };
593
594 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
589 } 595 }
590 ast::ExprKind::TryBlockExpr(e) => { 596 ast::ExprKind::TryBlockExpr(e) => {
591 let body = self.collect_block_opt(e.try_body()); 597 let body = self.collect_block_opt(e.try_body());