Skip to content

Commit 0618a6f

Browse files
bors[bot]feniljain
andauthored
Merge #10436
10436: fix: await insertion with try_expr during extract_function r=Veykril a=feniljain Fixing #10333 Co-authored-by: vi_mi <[email protected]>
2 parents 13ec077 + 6164351 commit 0618a6f

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ fn make_call(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String
10901090

10911091
let args = make::arg_list(fun.params.iter().map(|param| param.to_arg(ctx)));
10921092
let name = fun.name.clone();
1093-
let call_expr = if fun.self_param.is_some() {
1093+
let mut call_expr = if fun.self_param.is_some() {
10941094
let self_arg = make::expr_path(make::ext::ident_path("self"));
10951095
make::expr_method_call(self_arg, name, args)
10961096
} else {
@@ -1100,6 +1100,9 @@ fn make_call(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String
11001100

11011101
let handler = FlowHandler::from_ret_ty(fun, &ret_ty);
11021102

1103+
if fun.control_flow.is_async {
1104+
call_expr = make::expr_await(call_expr);
1105+
}
11031106
let expr = handler.make_call_expr(call_expr).indent(indent);
11041107

11051108
let mut_modifier = |var: &OutlivedLocal| if var.mut_usage_outside_body { "mut " } else { "" };
@@ -1119,10 +1122,8 @@ fn make_call(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String
11191122
buf.push_str(") = ");
11201123
}
11211124
}
1125+
11221126
format_to!(buf, "{}", expr);
1123-
if fun.control_flow.is_async {
1124-
buf.push_str(".await");
1125-
}
11261127
let insert_comma = fun
11271128
.body
11281129
.parent()
@@ -3870,6 +3871,70 @@ async fn $0fun_name() {
38703871
38713872
async fn some_function() {
38723873
3874+
}
3875+
"#,
3876+
);
3877+
}
3878+
3879+
#[test]
3880+
fn extract_with_await_and_result_not_producing_match_expr() {
3881+
check_assist(
3882+
extract_function,
3883+
r#"
3884+
async fn foo() -> Result<(), ()> {
3885+
$0async {}.await;
3886+
Err(())?$0
3887+
}
3888+
"#,
3889+
r#"
3890+
async fn foo() -> Result<(), ()> {
3891+
fun_name().await?
3892+
}
3893+
3894+
async fn $0fun_name() -> _ {
3895+
async {}.await;
3896+
Err(())?
3897+
}
3898+
"#,
3899+
);
3900+
}
3901+
3902+
#[test]
3903+
fn extract_with_await_and_result_producing_match_expr() {
3904+
check_assist(
3905+
extract_function,
3906+
r#"
3907+
async fn foo() -> i32 {
3908+
loop {
3909+
let n = 1;$0
3910+
let k = async { 1 }.await;
3911+
if k == 42 {
3912+
break 3;
3913+
}
3914+
let m = k + 1;$0
3915+
let h = 1 + m;
3916+
}
3917+
}
3918+
"#,
3919+
r#"
3920+
async fn foo() -> i32 {
3921+
loop {
3922+
let n = 1;
3923+
let m = match fun_name().await {
3924+
Ok(value) => value,
3925+
Err(value) => break value,
3926+
};
3927+
let h = 1 + m;
3928+
}
3929+
}
3930+
3931+
async fn $0fun_name() -> Result<i32, i32> {
3932+
let k = async { 1 }.await;
3933+
if k == 42 {
3934+
return Err(3);
3935+
}
3936+
let m = k + 1;
3937+
Ok(m)
38733938
}
38743939
"#,
38753940
);

crates/syntax/src/ast/make.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ pub fn expr_return(expr: Option<ast::Expr>) -> ast::Expr {
299299
pub fn expr_try(expr: ast::Expr) -> ast::Expr {
300300
expr_from_text(&format!("{}?", expr))
301301
}
302+
pub fn expr_await(expr: ast::Expr) -> ast::Expr {
303+
expr_from_text(&format!("{}.await", expr))
304+
}
302305
pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr {
303306
expr_from_text(&format!("match {} {}", expr, match_arm_list))
304307
}

0 commit comments

Comments
 (0)