From 2e6e5d8f7330c7970aa41e452a9fd9e3b3de4d84 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 6 Nov 2020 02:17:34 +0100 Subject: Ignore unit expressions in infer_function_return_type assist --- .../assists/src/handlers/infer_function_return_type.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'crates') 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}; // ``` pub(crate) fn infer_function_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let expr = ctx.find_node_at_offset::()?; - let (tail_expr, insert_pos) = extract(expr)?; + let (tail_expr, insert_pos) = extract_tail(expr)?; let module = ctx.sema.scope(tail_expr.syntax()).module()?; - let ty = ctx.sema.type_of_expr(&tail_expr)?; + let ty = ctx.sema.type_of_expr(&tail_expr).filter(|ty| !ty.is_unit())?; let ty = ty.display_source_code(ctx.db(), module.into()).ok()?; acc.add( @@ -34,7 +34,7 @@ pub(crate) fn infer_function_return_type(acc: &mut Assists, ctx: &AssistContext) ) } -fn extract(expr: ast::Expr) -> Option<(ast::Expr, SyntaxToken)> { +fn extract_tail(expr: ast::Expr) -> Option<(ast::Expr, SyntaxToken)> { let (ret_ty, tail_expr, insert_pos) = if let Some(closure) = expr.syntax().ancestors().find_map(ast::ClosureExpr::cast) { let tail_expr = match closure.body()? { @@ -127,6 +127,16 @@ mod tests { ); } + #[test] + fn not_applicable_unit_return_type() { + check_assist_not_applicable( + infer_function_return_type, + r#"fn foo() { + (<|>) + }"#, + ); + } + #[test] fn infer_return_type_closure_block() { check_assist( -- cgit v1.2.3