aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/assists/src/handlers/infer_function_return_type.rs16
1 files changed, 13 insertions, 3 deletions
diff --git a/crates/assists/src/handlers/infer_function_return_type.rs b/crates/assists/src/handlers/infer_function_return_type.rs
index f363a56f3..81217378a 100644
--- a/crates/assists/src/handlers/infer_function_return_type.rs
+++ b/crates/assists/src/handlers/infer_function_return_type.rs
@@ -18,9 +18,9 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
18// ``` 18// ```
19pub(crate) fn infer_function_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 19pub(crate) fn infer_function_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
20 let expr = ctx.find_node_at_offset::<ast::Expr>()?; 20 let expr = ctx.find_node_at_offset::<ast::Expr>()?;
21 let (tail_expr, insert_pos) = extract(expr)?; 21 let (tail_expr, insert_pos) = extract_tail(expr)?;
22 let module = ctx.sema.scope(tail_expr.syntax()).module()?; 22 let module = ctx.sema.scope(tail_expr.syntax()).module()?;
23 let ty = ctx.sema.type_of_expr(&tail_expr)?; 23 let ty = ctx.sema.type_of_expr(&tail_expr).filter(|ty| !ty.is_unit())?;
24 let ty = ty.display_source_code(ctx.db(), module.into()).ok()?; 24 let ty = ty.display_source_code(ctx.db(), module.into()).ok()?;
25 25
26 acc.add( 26 acc.add(
@@ -34,7 +34,7 @@ pub(crate) fn infer_function_return_type(acc: &mut Assists, ctx: &AssistContext)
34 ) 34 )
35} 35}
36 36
37fn extract(expr: ast::Expr) -> Option<(ast::Expr, SyntaxToken)> { 37fn extract_tail(expr: ast::Expr) -> Option<(ast::Expr, SyntaxToken)> {
38 let (ret_ty, tail_expr, insert_pos) = 38 let (ret_ty, tail_expr, insert_pos) =
39 if let Some(closure) = expr.syntax().ancestors().find_map(ast::ClosureExpr::cast) { 39 if let Some(closure) = expr.syntax().ancestors().find_map(ast::ClosureExpr::cast) {
40 let tail_expr = match closure.body()? { 40 let tail_expr = match closure.body()? {
@@ -128,6 +128,16 @@ mod tests {
128 } 128 }
129 129
130 #[test] 130 #[test]
131 fn not_applicable_unit_return_type() {
132 check_assist_not_applicable(
133 infer_function_return_type,
134 r#"fn foo() {
135 (<|>)
136 }"#,
137 );
138 }
139
140 #[test]
131 fn infer_return_type_closure_block() { 141 fn infer_return_type_closure_block() {
132 check_assist( 142 check_assist(
133 infer_function_return_type, 143 infer_function_return_type,