diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-05-10 13:26:52 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-05-10 13:26:52 +0100 |
commit | 619eb96153743c3c4406f1cdd0f85b82abea1587 (patch) | |
tree | 9970300ac753fa7856defc709a73fc886d55880c | |
parent | fd109fb587904cfecc1149e068814bfd38feb83c (diff) | |
parent | bf26e13cd252a6337aa290adf842bf72e2523c4d (diff) |
Merge #8789
8789: simplify r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r-- | crates/ide_assists/src/handlers/replace_let_with_if_let.rs | 21 | ||||
-rw-r--r-- | crates/syntax/src/ast/make.rs | 3 |
2 files changed, 12 insertions, 12 deletions
diff --git a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs index f811234c0..1ad0fa816 100644 --- a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs +++ b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs | |||
@@ -50,22 +50,19 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> | |||
50 | "Replace with if-let", | 50 | "Replace with if-let", |
51 | target, | 51 | target, |
52 | |edit| { | 52 | |edit| { |
53 | let with_placeholder: ast::Pat = match happy_variant { | 53 | let pat = match happy_variant { |
54 | None => make::wildcard_pat().into(), | 54 | None => original_pat, |
55 | Some(var_name) => make::tuple_struct_pat( | 55 | Some(var_name) => { |
56 | make::ext::ident_path(var_name), | 56 | make::tuple_struct_pat(make::ext::ident_path(var_name), once(original_pat)) |
57 | once(make::wildcard_pat().into()), | 57 | .into() |
58 | ) | 58 | } |
59 | .into(), | ||
60 | }; | 59 | }; |
60 | |||
61 | let block = | 61 | let block = |
62 | make::block_expr(None, None).indent(IndentLevel::from_node(let_stmt.syntax())); | 62 | make::ext::empty_block_expr().indent(IndentLevel::from_node(let_stmt.syntax())); |
63 | let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block, None); | 63 | let if_ = make::expr_if(make::condition(init, Some(pat)), block, None); |
64 | let stmt = make::expr_stmt(if_); | 64 | let stmt = make::expr_stmt(if_); |
65 | 65 | ||
66 | let placeholder = stmt.syntax().descendants().find_map(ast::WildcardPat::cast).unwrap(); | ||
67 | let stmt = stmt.replace_descendant(placeholder.into(), original_pat); | ||
68 | |||
69 | edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt)); | 66 | edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt)); |
70 | }, | 67 | }, |
71 | ) | 68 | ) |
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index a378b1d37..1998ad1f6 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -31,6 +31,9 @@ pub mod ext { | |||
31 | pub fn expr_todo() -> ast::Expr { | 31 | pub fn expr_todo() -> ast::Expr { |
32 | expr_from_text("todo!()") | 32 | expr_from_text("todo!()") |
33 | } | 33 | } |
34 | pub fn empty_block_expr() -> ast::BlockExpr { | ||
35 | block_expr(None, None) | ||
36 | } | ||
34 | 37 | ||
35 | pub fn ty_bool() -> ast::Type { | 38 | pub fn ty_bool() -> ast::Type { |
36 | ty_path(ident_path("bool")) | 39 | ty_path(ident_path("bool")) |