Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 03c5dd1

Browse files
committed
extract_function: use appropriate return type for async fns
1 parent 9d787e1 commit 03c5dd1

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,14 @@ impl FunctionBody {
692692
(constness, expr.clone(), infer_expr_opt(expr))
693693
},
694694
ast::Fn(fn_) => {
695-
(fn_.const_token().is_some(), fn_.body().map(ast::Expr::BlockExpr), Some(sema.to_def(&fn_)?.ret_type(sema.db)))
695+
let func = sema.to_def(&fn_)?;
696+
let mut ret_ty = func.ret_type(sema.db);
697+
if func.is_async(sema.db) {
698+
if let Some(async_ret) = func.async_ret_type(sema.db) {
699+
ret_ty = async_ret;
700+
}
701+
}
702+
(fn_.const_token().is_some(), fn_.body().map(ast::Expr::BlockExpr), Some(ret_ty))
696703
},
697704
ast::Static(statik) => {
698705
(true, statik.body(), Some(sema.to_def(&statik)?.ty(sema.db)))
@@ -4026,6 +4033,7 @@ fn $0fun_name(n: i32) -> i32 {
40264033
check_assist(
40274034
extract_function,
40284035
r#"
4036+
//- minicore: future
40294037
fn main() {
40304038
$0some_function().await;$0
40314039
}
@@ -4055,6 +4063,7 @@ async fn some_function() {
40554063
check_assist(
40564064
extract_function,
40574065
r#"
4066+
//- minicore: future, result
40584067
async fn foo() -> Result<(), ()> {
40594068
$0async {}.await;
40604069
Err(())?$0
@@ -4065,7 +4074,7 @@ async fn foo() -> Result<(), ()> {
40654074
fun_name().await?
40664075
}
40674076
4068-
async fn $0fun_name() -> _ {
4077+
async fn $0fun_name() -> Result<(), ()> {
40694078
async {}.await;
40704079
Err(())?
40714080
}
@@ -4078,6 +4087,7 @@ async fn $0fun_name() -> _ {
40784087
check_assist(
40794088
extract_function,
40804089
r#"
4090+
//- minicore: future
40814091
async fn foo() -> i32 {
40824092
loop {
40834093
let n = 1;$0
@@ -4119,6 +4129,7 @@ async fn $0fun_name() -> Result<i32, i32> {
41194129
check_assist(
41204130
extract_function,
41214131
r#"
4132+
//- minicore: future
41224133
fn main() {
41234134
$0function_call("a", some_function().await);$0
41244135
}

0 commit comments

Comments
 (0)