]> git.lizzy.rs Git - rust.git/commitdiff
add_explicit_type respects `@` patterns
authorDawer <7803845+iDawer@users.noreply.github.com>
Wed, 19 May 2021 18:12:09 +0000 (23:12 +0500)
committerDawer <7803845+iDawer@users.noreply.github.com>
Wed, 19 May 2021 18:27:51 +0000 (23:27 +0500)
crates/ide_assists/src/handlers/add_explicit_type.rs

index 36589203d5a97b2f894aa7f77261dfbd52ed8ffe..b7617ca3da8c14e251db1ba6ff1a5a9379c36673 100644 (file)
@@ -1,6 +1,6 @@
 use hir::HirDisplay;
 use syntax::{
-    ast::{self, AstNode, LetStmt, NameOwner},
+    ast::{self, AstNode, LetStmt},
     TextRange,
 };
 
@@ -31,9 +31,6 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
         _ => return None,
     };
     let pat_range = pat.syntax().text_range();
-    // The binding must have a name
-    let name = pat.name()?;
-    let name_range = name.syntax().text_range();
 
     // Assist should only be applicable if cursor is between 'let' and '='
     let cursor_in_range = {
@@ -74,7 +71,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
                 builder.replace(ascribed_ty.syntax().text_range(), inferred_type);
             }
             None => {
-                builder.insert(name_range.end(), format!(": {}", inferred_type));
+                builder.insert(pat_range.end(), format!(": {}", inferred_type));
             }
         },
     )
@@ -243,6 +240,24 @@ struct Test<K, T = u8> { k: K, t: T }
 fn main() {
     let test: Test<i32> = Test { t: 23u8, k: 33 };
 }
+"#,
+        );
+    }
+
+    #[test]
+    fn type_should_be_added_after_pattern() {
+        // LetStmt = Attr* 'let' Pat (':' Type)? '=' initializer:Expr ';'
+        check_assist(
+            add_explicit_type,
+            r#"
+fn main() {
+    let $0test @ () = ();
+}
+"#,
+            r#"
+fn main() {
+    let test @ (): () = ();
+}
 "#,
         );
     }