diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-12-07 15:00:07 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-12-07 15:00:07 +0000 |
commit | 6df91a84dc129b13dc58db510e68c5eb04297087 (patch) | |
tree | 277812e8a9562861925fad7bd7c91321bf79c4f7 /crates/syntax/src/ast | |
parent | 403ed489ff51e4b1d9b1bbde1ddb6f765ebcbd1f (diff) | |
parent | 44c76d6550081552c3c5106b0535a7e5bf265aec (diff) |
Merge #6731
6731: Add replace_match_with_if_let assist r=matklad a=Veykril
Basically the counterpart to `replace_if_let_with_match`, I personally sometimes want to replace matches like
```rust
match foo {
pat => expr,
_ => (),
}
```
into the corresponding
```rust
if let pat = foo {
expr
}
```
which is the main reasoning behind this.
I put this into the same file as `replace_if_let_with_match` because the are complementing each other and I would probably rename the file to something like `replace_if_let_match` but I didn't do that for now because I was unsure whether git would still view this as a rename or not due to the amount of changes in the file so that the diff is still properly visible for now.
Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/syntax/src/ast')
-rw-r--r-- | crates/syntax/src/ast/make.rs | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 876659a2b..cc09b77a5 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -171,8 +171,17 @@ pub fn expr_return() -> ast::Expr { | |||
171 | pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { | 171 | pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { |
172 | expr_from_text(&format!("match {} {}", expr, match_arm_list)) | 172 | expr_from_text(&format!("match {} {}", expr, match_arm_list)) |
173 | } | 173 | } |
174 | pub fn expr_if(condition: ast::Condition, then_branch: ast::BlockExpr) -> ast::Expr { | 174 | pub fn expr_if( |
175 | expr_from_text(&format!("if {} {}", condition, then_branch)) | 175 | condition: ast::Condition, |
176 | then_branch: ast::BlockExpr, | ||
177 | else_branch: Option<ast::ElseBranch>, | ||
178 | ) -> ast::Expr { | ||
179 | let else_branch = match else_branch { | ||
180 | Some(ast::ElseBranch::Block(block)) => format!("else {}", block), | ||
181 | Some(ast::ElseBranch::IfExpr(if_expr)) => format!("else {}", if_expr), | ||
182 | None => String::new(), | ||
183 | }; | ||
184 | expr_from_text(&format!("if {} {} {}", condition, then_branch, else_branch)) | ||
176 | } | 185 | } |
177 | pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { | 186 | pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { |
178 | let token = token(op); | 187 | let token = token(op); |