Skip to content

Commit bf26e13

Browse files
committed
simplify
1 parent fd109fb commit bf26e13

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

crates/ide_assists/src/handlers/replace_let_with_if_let.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,19 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
5050
"Replace with if-let",
5151
target,
5252
|edit| {
53-
let with_placeholder: ast::Pat = match happy_variant {
54-
None => make::wildcard_pat().into(),
55-
Some(var_name) => make::tuple_struct_pat(
56-
make::ext::ident_path(var_name),
57-
once(make::wildcard_pat().into()),
58-
)
59-
.into(),
53+
let pat = match happy_variant {
54+
None => original_pat,
55+
Some(var_name) => {
56+
make::tuple_struct_pat(make::ext::ident_path(var_name), once(original_pat))
57+
.into()
58+
}
6059
};
60+
6161
let block =
62-
make::block_expr(None, None).indent(IndentLevel::from_node(let_stmt.syntax()));
63-
let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block, None);
62+
make::ext::empty_block_expr().indent(IndentLevel::from_node(let_stmt.syntax()));
63+
let if_ = make::expr_if(make::condition(init, Some(pat)), block, None);
6464
let stmt = make::expr_stmt(if_);
6565

66-
let placeholder = stmt.syntax().descendants().find_map(ast::WildcardPat::cast).unwrap();
67-
let stmt = stmt.replace_descendant(placeholder.into(), original_pat);
68-
6966
edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt));
7067
},
7168
)

crates/syntax/src/ast/make.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub mod ext {
3131
pub fn expr_todo() -> ast::Expr {
3232
expr_from_text("todo!()")
3333
}
34+
pub fn empty_block_expr() -> ast::BlockExpr {
35+
block_expr(None, None)
36+
}
3437

3538
pub fn ty_bool() -> ast::Type {
3639
ty_path(ident_path("bool"))

0 commit comments

Comments
 (0)