mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-03 20:23:59 +00:00
simplify
This commit is contained in:
parent
fd109fb587
commit
bf26e13cd2
@ -50,21 +50,18 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
|
|||||||
"Replace with if-let",
|
"Replace with if-let",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
let with_placeholder: ast::Pat = match happy_variant {
|
let pat = match happy_variant {
|
||||||
None => make::wildcard_pat().into(),
|
None => original_pat,
|
||||||
Some(var_name) => make::tuple_struct_pat(
|
Some(var_name) => {
|
||||||
make::ext::ident_path(var_name),
|
make::tuple_struct_pat(make::ext::ident_path(var_name), once(original_pat))
|
||||||
once(make::wildcard_pat().into()),
|
.into()
|
||||||
)
|
}
|
||||||
.into(),
|
|
||||||
};
|
};
|
||||||
let block =
|
|
||||||
make::block_expr(None, None).indent(IndentLevel::from_node(let_stmt.syntax()));
|
|
||||||
let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block, None);
|
|
||||||
let stmt = make::expr_stmt(if_);
|
|
||||||
|
|
||||||
let placeholder = stmt.syntax().descendants().find_map(ast::WildcardPat::cast).unwrap();
|
let block =
|
||||||
let stmt = stmt.replace_descendant(placeholder.into(), original_pat);
|
make::ext::empty_block_expr().indent(IndentLevel::from_node(let_stmt.syntax()));
|
||||||
|
let if_ = make::expr_if(make::condition(init, Some(pat)), block, None);
|
||||||
|
let stmt = make::expr_stmt(if_);
|
||||||
|
|
||||||
edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt));
|
edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt));
|
||||||
},
|
},
|
||||||
|
@ -31,6 +31,9 @@ pub mod ext {
|
|||||||
pub fn expr_todo() -> ast::Expr {
|
pub fn expr_todo() -> ast::Expr {
|
||||||
expr_from_text("todo!()")
|
expr_from_text("todo!()")
|
||||||
}
|
}
|
||||||
|
pub fn empty_block_expr() -> ast::BlockExpr {
|
||||||
|
block_expr(None, None)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ty_bool() -> ast::Type {
|
pub fn ty_bool() -> ast::Type {
|
||||||
ty_path(ident_path("bool"))
|
ty_path(ident_path("bool"))
|
||||||
|
Loading…
Reference in New Issue
Block a user