diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-09-02 20:20:24 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-09-02 20:20:24 +0100 |
commit | 7faec1c30046769d4ae490e15cf5405bcfbdeef8 (patch) | |
tree | 6d268b721027a5350928a6c5a0ec227b5fde8ebc /crates/ra_assists/src/replace_if_let_with_match.rs | |
parent | a8397deab914240aca8f015fb3736689919c0a5b (diff) | |
parent | e94587e3153b52684fd3f6b82c8e7efc09ff5c8d (diff) |
Merge #1752
1752: Always wrap blocks into block expressions r=flodiebold a=matklad
This way, things like function bodies are expressions, and we don't have to single them out
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/replace_if_let_with_match.rs')
-rw-r--r-- | crates/ra_assists/src/replace_if_let_with_match.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/crates/ra_assists/src/replace_if_let_with_match.rs b/crates/ra_assists/src/replace_if_let_with_match.rs index c0bf6d235..401835c57 100644 --- a/crates/ra_assists/src/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/replace_if_let_with_match.rs | |||
@@ -1,3 +1,4 @@ | |||
1 | use format_buf::format; | ||
1 | use hir::db::HirDatabase; | 2 | use hir::db::HirDatabase; |
2 | use ra_fmt::extract_trivial_expression; | 3 | use ra_fmt::extract_trivial_expression; |
3 | use ra_syntax::{ast, AstNode}; | 4 | use ra_syntax::{ast, AstNode}; |
@@ -25,16 +26,21 @@ pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx<impl HirDatabase>) -> | |||
25 | ctx.build() | 26 | ctx.build() |
26 | } | 27 | } |
27 | 28 | ||
28 | fn build_match_expr(expr: ast::Expr, pat1: ast::Pat, arm1: ast::Block, arm2: ast::Block) -> String { | 29 | fn build_match_expr( |
30 | expr: ast::Expr, | ||
31 | pat1: ast::Pat, | ||
32 | arm1: ast::BlockExpr, | ||
33 | arm2: ast::BlockExpr, | ||
34 | ) -> String { | ||
29 | let mut buf = String::new(); | 35 | let mut buf = String::new(); |
30 | buf.push_str(&format!("match {} {{\n", expr.syntax().text())); | 36 | format!(buf, "match {} {{\n", expr.syntax().text()); |
31 | buf.push_str(&format!(" {} => {}\n", pat1.syntax().text(), format_arm(&arm1))); | 37 | format!(buf, " {} => {}\n", pat1.syntax().text(), format_arm(&arm1)); |
32 | buf.push_str(&format!(" _ => {}\n", format_arm(&arm2))); | 38 | format!(buf, " _ => {}\n", format_arm(&arm2)); |
33 | buf.push_str("}"); | 39 | buf.push_str("}"); |
34 | buf | 40 | buf |
35 | } | 41 | } |
36 | 42 | ||
37 | fn format_arm(block: &ast::Block) -> String { | 43 | fn format_arm(block: &ast::BlockExpr) -> String { |
38 | match extract_trivial_expression(block) { | 44 | match extract_trivial_expression(block) { |
39 | None => block.syntax().text().to_string(), | 45 | None => block.syntax().text().to_string(), |
40 | Some(e) => format!("{},", e.syntax().text()), | 46 | Some(e) => format!("{},", e.syntax().text()), |