extract_function: use appropriate return type for async fns

This commit is contained in:
iDawer 2022-04-14 21:12:26 +05:00
parent 9d787e1bfe
commit 03c5dd1252

View File

@ -692,7 +692,14 @@ fn analyze_container(&self, sema: &Semantics<RootDatabase>) -> Option<ContainerI
(constness, expr.clone(), infer_expr_opt(expr))
},
ast::Fn(fn_) => {
(fn_.const_token().is_some(), fn_.body().map(ast::Expr::BlockExpr), Some(sema.to_def(&fn_)?.ret_type(sema.db)))
let func = sema.to_def(&fn_)?;
let mut ret_ty = func.ret_type(sema.db);
if func.is_async(sema.db) {
if let Some(async_ret) = func.async_ret_type(sema.db) {
ret_ty = async_ret;
}
}
(fn_.const_token().is_some(), fn_.body().map(ast::Expr::BlockExpr), Some(ret_ty))
},
ast::Static(statik) => {
(true, statik.body(), Some(sema.to_def(&statik)?.ty(sema.db)))
@ -4026,6 +4033,7 @@ fn extract_with_await() {
check_assist(
extract_function,
r#"
//- minicore: future
fn main() {
$0some_function().await;$0
}
@ -4055,6 +4063,7 @@ fn extract_with_await_and_result_not_producing_match_expr() {
check_assist(
extract_function,
r#"
//- minicore: future, result
async fn foo() -> Result<(), ()> {
$0async {}.await;
Err(())?$0
@ -4065,7 +4074,7 @@ async fn foo() -> Result<(), ()> {
fun_name().await?
}
async fn $0fun_name() -> _ {
async fn $0fun_name() -> Result<(), ()> {
async {}.await;
Err(())?
}
@ -4078,6 +4087,7 @@ fn extract_with_await_and_result_producing_match_expr() {
check_assist(
extract_function,
r#"
//- minicore: future
async fn foo() -> i32 {
loop {
let n = 1;$0
@ -4119,6 +4129,7 @@ fn extract_with_await_in_args() {
check_assist(
extract_function,
r#"
//- minicore: future
fn main() {
$0function_call("a", some_function().await);$0
}