From 3d9d10be396d9a5ddfdc602a4283054924cfbcc6 Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Sat, 4 Sep 2021 14:21:14 +0500 Subject: [PATCH] fix: use placeholder as default type in `Extract into function`. --- .../src/handlers/extract_function.rs | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index 192403b15d77..1e21050911d7 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs @@ -1319,7 +1319,7 @@ impl Function { .type_arguments() .nth(1) .map(|ty| make_ty(&ty, ctx, module)) - .unwrap_or_else(make::ty_unit); + .unwrap_or_else(make::ty_placeholder); make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty) } FlowHandler::If { .. } => make::ext::ty_bool(), @@ -1327,7 +1327,7 @@ impl Function { let handler_ty = action .expr_ty(ctx) .map(|ty| make_ty(&ty, ctx, module)) - .unwrap_or_else(make::ty_unit); + .unwrap_or_else(make::ty_placeholder); make::ext::ty_option(handler_ty) } FlowHandler::MatchOption { .. } => make::ext::ty_option(fun_ty.make_ty(ctx, module)), @@ -1335,7 +1335,7 @@ impl Function { let handler_ty = err .expr_ty(ctx) .map(|ty| make_ty(&ty, ctx, module)) - .unwrap_or_else(make::ty_unit); + .unwrap_or_else(make::ty_placeholder); make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty) } }; @@ -1501,7 +1501,7 @@ fn with_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr } fn format_type(ty: &hir::Type, ctx: &AssistContext, module: hir::Module) -> String { - ty.display_source_code(ctx.db(), module.into()).ok().unwrap_or_else(|| "()".to_string()) + ty.display_source_code(ctx.db(), module.into()).ok().unwrap_or_else(|| "_".to_string()) } fn make_ty(ty: &hir::Type, ctx: &AssistContext, module: hir::Module) -> ast::Type { @@ -4191,6 +4191,29 @@ fn main() { fn $0fun_name(bar: &str) { m!(bar); } +"#, + ); + } + + #[test] + fn unresolveable_types_default_to_placeholder() { + check_assist( + extract_function, + r#" +fn foo() { + let a = __unresolved; + let _ = $0{a}$0; +} +"#, + r#" +fn foo() { + let a = __unresolved; + let _ = fun_name(a); +} + +fn $0fun_name(a: _) -> _ { + a +} "#, ); }