Ignore unit expressions in infer_function_return_type assist

This commit is contained in:
Lukas Wirth 2020-11-06 02:17:34 +01:00
parent 0a7c8512ff
commit 2e6e5d8f73

View File

@ -18,9 +18,9 @@
// ```
pub(crate) fn infer_function_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let expr = ctx.find_node_at_offset::<ast::Expr>()?;
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 @@ fn not_applicable_non_tail_expr() {
);
}
#[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(