From ce06a6b4225d7ea5777da11166ad618389df3573 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 21 Apr 2020 22:56:11 +0300 Subject: [PATCH] Do not add default and closure types in 'add explicit type' assist --- .../src/handlers/add_explicit_type.rs | 48 +++++++++++++++++-- crates/ra_hir/src/code_model.rs | 4 ++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs index d86d804b2e..6c56d93d87 100644 --- a/crates/ra_assists/src/handlers/add_explicit_type.rs +++ b/crates/ra_assists/src/handlers/add_explicit_type.rs @@ -52,21 +52,22 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option { } // Infer type let ty = ctx.sema.type_of_expr(&expr)?; - // Assist not applicable if the type is unknown - if ty.contains_unknown() { + + if ty.contains_unknown() || ty.is_closure() { return None; } let db = ctx.db; + let new_type_string = ty.display_truncated(db, None).to_string(); ctx.add_assist( AssistId("add_explicit_type"), - format!("Insert explicit type '{}'", ty.display(db)), + format!("Insert explicit type '{}'", new_type_string), |edit| { edit.target(pat_range); if let Some(ascribed_ty) = ascribed_ty { - edit.replace(ascribed_ty.syntax().text_range(), format!("{}", ty.display(db))); + edit.replace(ascribed_ty.syntax().text_range(), new_type_string); } else { - edit.insert(name_range.end(), format!(": {}", ty.display(db))); + edit.insert(name_range.end(), format!(": {}", new_type_string)); } }, ) @@ -174,4 +175,41 @@ mod tests { "fn f() <|>{let a = match 1 {2 => 3, 3 => 5};}", ) } + + #[test] + fn closure_parameters_are_not_added() { + check_assist_not_applicable( + add_explicit_type, + r#" +fn main() { + let multiply_by_two<|> = |i| i * 3; + let six = multiply_by_two(2); +}"#, + ) + } + + #[test] + fn default_generics_should_not_be_added() { + check_assist( + add_explicit_type, + r#" +struct Test { + k: K, + t: T, +} + +fn main() { + let test<|> = Test { t: 23, k: 33 }; +}"#, + r#" +struct Test { + k: K, + t: T, +} + +fn main() { + let test<|>: Test = Test { t: 23, k: 33 }; +}"#, + ); + } } diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 6e0d89466b..43f932e20c 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1132,6 +1132,10 @@ impl Type { Some(self.ty.value.as_callable()?.0) } + pub fn is_closure(&self) -> bool { + matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Closure { .. }, .. })) + } + pub fn contains_unknown(&self) -> bool { return go(&self.ty.value);