mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
refactor if-let lowering
mainly to get rid of unwraps
This commit is contained in:
parent
4d6475ada0
commit
39967a85e1
1 changed files with 25 additions and 19 deletions
|
@ -559,6 +559,7 @@ where
|
||||||
match expr.kind() {
|
match expr.kind() {
|
||||||
ast::ExprKind::IfExpr(e) => {
|
ast::ExprKind::IfExpr(e) => {
|
||||||
let then_branch = self.collect_block_opt(e.then_branch());
|
let then_branch = self.collect_block_opt(e.then_branch());
|
||||||
|
|
||||||
let else_branch = e.else_branch().map(|b| match b {
|
let else_branch = e.else_branch().map(|b| match b {
|
||||||
ast::ElseBranch::Block(it) => self.collect_block(it),
|
ast::ElseBranch::Block(it) => self.collect_block(it),
|
||||||
ast::ElseBranch::IfExpr(elif) => {
|
ast::ElseBranch::IfExpr(elif) => {
|
||||||
|
@ -567,25 +568,30 @@ where
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(pat) = e.condition().and_then(|c| c.pat()) {
|
let condition = match e.condition() {
|
||||||
// if let -- desugar to match
|
None => self.exprs.alloc(Expr::Missing),
|
||||||
let pat = self.collect_pat(pat);
|
Some(condition) => match condition.pat() {
|
||||||
let match_expr =
|
None => self.collect_expr_opt(condition.expr()),
|
||||||
self.collect_expr_opt(e.condition().expect("checked above").expr());
|
// if let -- desugar to match
|
||||||
let placeholder_pat = self.pats.alloc(Pat::Missing);
|
Some(pat) => {
|
||||||
let arms = vec![
|
let pat = self.collect_pat(pat);
|
||||||
MatchArm { pats: vec![pat], expr: then_branch, guard: None },
|
let match_expr = self.collect_expr_opt(condition.expr());
|
||||||
MatchArm {
|
let placeholder_pat = self.pats.alloc(Pat::Missing);
|
||||||
pats: vec![placeholder_pat],
|
let arms = vec![
|
||||||
expr: else_branch.unwrap_or_else(|| self.empty_block()),
|
MatchArm { pats: vec![pat], expr: then_branch, guard: None },
|
||||||
guard: None,
|
MatchArm {
|
||||||
},
|
pats: vec![placeholder_pat],
|
||||||
];
|
expr: else_branch.unwrap_or_else(|| self.empty_block()),
|
||||||
self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr)
|
guard: None,
|
||||||
} else {
|
},
|
||||||
let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr()));
|
];
|
||||||
self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
|
return self
|
||||||
}
|
.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
|
||||||
}
|
}
|
||||||
ast::ExprKind::TryBlockExpr(e) => {
|
ast::ExprKind::TryBlockExpr(e) => {
|
||||||
let body = self.collect_block_opt(e.try_body());
|
let body = self.collect_block_opt(e.try_body());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue