mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
use ControlFlow
in "extract function" assist
This commit is contained in:
parent
137ac67f5d
commit
971a271840
1 changed files with 30 additions and 13 deletions
|
@ -1184,7 +1184,18 @@ impl FlowHandler {
|
||||||
let action = action.make_result_handler(None);
|
let action = action.make_result_handler(None);
|
||||||
let stmt = make::expr_stmt(action);
|
let stmt = make::expr_stmt(action);
|
||||||
let block = make::block_expr(iter::once(stmt.into()), None);
|
let block = make::block_expr(iter::once(stmt.into()), None);
|
||||||
let condition = make::condition(call_expr, None);
|
let controlflow_break_path = make::path_from_text("ControlFlow::Break");
|
||||||
|
let tuple_pat = make::tuple_pat(iter::empty());
|
||||||
|
let condition = make::condition(
|
||||||
|
call_expr,
|
||||||
|
Some(
|
||||||
|
make::tuple_struct_pat(
|
||||||
|
controlflow_break_path,
|
||||||
|
iter::once(tuple_pat.into()),
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
|
),
|
||||||
|
);
|
||||||
make::expr_if(condition, block, None)
|
make::expr_if(condition, block, None)
|
||||||
}
|
}
|
||||||
FlowHandler::IfOption { action } => {
|
FlowHandler::IfOption { action } => {
|
||||||
|
@ -1326,7 +1337,7 @@ impl Function {
|
||||||
.unwrap_or_else(make::ty_placeholder);
|
.unwrap_or_else(make::ty_placeholder);
|
||||||
make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty)
|
make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty)
|
||||||
}
|
}
|
||||||
FlowHandler::If { .. } => make::ext::ty_bool(),
|
FlowHandler::If { .. } => make::ty("ControlFlow<()>"),
|
||||||
FlowHandler::IfOption { action } => {
|
FlowHandler::IfOption { action } => {
|
||||||
let handler_ty = action
|
let handler_ty = action
|
||||||
.expr_ty(ctx)
|
.expr_ty(ctx)
|
||||||
|
@ -1461,8 +1472,11 @@ fn make_body(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
FlowHandler::If { .. } => {
|
FlowHandler::If { .. } => {
|
||||||
let lit_false = make::expr_literal("false");
|
let controlflow_continue = make::expr_call(
|
||||||
with_tail_expr(block, lit_false.into())
|
make::expr_path(make::path_from_text("ControlFlow::Continue")),
|
||||||
|
make::arg_list(iter::once(make::expr_unit())),
|
||||||
|
);
|
||||||
|
with_tail_expr(block, controlflow_continue.into())
|
||||||
}
|
}
|
||||||
FlowHandler::IfOption { .. } => {
|
FlowHandler::IfOption { .. } => {
|
||||||
let none = make::expr_path(make::ext::ident_path("None"));
|
let none = make::expr_path(make::ext::ident_path("None"));
|
||||||
|
@ -1638,7 +1652,10 @@ fn update_external_control_flow(handler: &FlowHandler, syntax: &SyntaxNode) {
|
||||||
fn make_rewritten_flow(handler: &FlowHandler, arg_expr: Option<ast::Expr>) -> Option<ast::Expr> {
|
fn make_rewritten_flow(handler: &FlowHandler, arg_expr: Option<ast::Expr>) -> Option<ast::Expr> {
|
||||||
let value = match handler {
|
let value = match handler {
|
||||||
FlowHandler::None | FlowHandler::Try { .. } => return None,
|
FlowHandler::None | FlowHandler::Try { .. } => return None,
|
||||||
FlowHandler::If { .. } => make::expr_literal("true").into(),
|
FlowHandler::If { .. } => make::expr_call(
|
||||||
|
make::expr_path(make::path_from_text("ControlFlow::Break")),
|
||||||
|
make::arg_list(iter::once(make::expr_unit())),
|
||||||
|
),
|
||||||
FlowHandler::IfOption { .. } => {
|
FlowHandler::IfOption { .. } => {
|
||||||
let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new()));
|
let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new()));
|
||||||
let args = make::arg_list(iter::once(expr));
|
let args = make::arg_list(iter::once(expr));
|
||||||
|
@ -3284,18 +3301,18 @@ fn foo() {
|
||||||
fn foo() {
|
fn foo() {
|
||||||
loop {
|
loop {
|
||||||
let mut n = 1;
|
let mut n = 1;
|
||||||
if fun_name(&mut n) {
|
if let ControlFlow::Break(()) = fun_name(&mut n) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let h = 1 + n;
|
let h = 1 + n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn $0fun_name(n: &mut i32) -> bool {
|
fn $0fun_name(n: &mut i32) -> ControlFlow<()> {
|
||||||
let m = *n + 1;
|
let m = *n + 1;
|
||||||
return true;
|
return ControlFlow::Break(());
|
||||||
*n += m;
|
*n += m;
|
||||||
false
|
ControlFlow::Continue(())
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
@ -3321,19 +3338,19 @@ fn foo() {
|
||||||
fn foo() {
|
fn foo() {
|
||||||
loop {
|
loop {
|
||||||
let mut n = 1;
|
let mut n = 1;
|
||||||
if fun_name(n) {
|
if let ControlFlow::Break(()) = fun_name(n) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let h = 1;
|
let h = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn $0fun_name(n: i32) -> bool {
|
fn $0fun_name(n: i32) -> ControlFlow<()> {
|
||||||
let m = n + 1;
|
let m = n + 1;
|
||||||
if m == 42 {
|
if m == 42 {
|
||||||
return true;
|
return ControlFlow::Break(());
|
||||||
}
|
}
|
||||||
false
|
ControlFlow::Continue(())
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue