aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_assists')
-rw-r--r--crates/ide_assists/src/handlers/add_explicit_type.rs25
1 files changed, 20 insertions, 5 deletions
diff --git a/crates/ide_assists/src/handlers/add_explicit_type.rs b/crates/ide_assists/src/handlers/add_explicit_type.rs
index 36589203d..b7617ca3d 100644
--- a/crates/ide_assists/src/handlers/add_explicit_type.rs
+++ b/crates/ide_assists/src/handlers/add_explicit_type.rs
@@ -1,6 +1,6 @@
1use hir::HirDisplay; 1use hir::HirDisplay;
2use syntax::{ 2use syntax::{
3 ast::{self, AstNode, LetStmt, NameOwner}, 3 ast::{self, AstNode, LetStmt},
4 TextRange, 4 TextRange,
5}; 5};
6 6
@@ -31,9 +31,6 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
31 _ => return None, 31 _ => return None,
32 }; 32 };
33 let pat_range = pat.syntax().text_range(); 33 let pat_range = pat.syntax().text_range();
34 // The binding must have a name
35 let name = pat.name()?;
36 let name_range = name.syntax().text_range();
37 34
38 // Assist should only be applicable if cursor is between 'let' and '=' 35 // Assist should only be applicable if cursor is between 'let' and '='
39 let cursor_in_range = { 36 let cursor_in_range = {
@@ -74,7 +71,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
74 builder.replace(ascribed_ty.syntax().text_range(), inferred_type); 71 builder.replace(ascribed_ty.syntax().text_range(), inferred_type);
75 } 72 }
76 None => { 73 None => {
77 builder.insert(name_range.end(), format!(": {}", inferred_type)); 74 builder.insert(pat_range.end(), format!(": {}", inferred_type));
78 } 75 }
79 }, 76 },
80 ) 77 )
@@ -246,4 +243,22 @@ fn main() {
246"#, 243"#,
247 ); 244 );
248 } 245 }
246
247 #[test]
248 fn type_should_be_added_after_pattern() {
249 // LetStmt = Attr* 'let' Pat (':' Type)? '=' initializer:Expr ';'
250 check_assist(
251 add_explicit_type,
252 r#"
253fn main() {
254 let $0test @ () = ();
255}
256"#,
257 r#"
258fn main() {
259 let test @ (): () = ();
260}
261"#,
262 );
263 }
249} 264}