diff options
Diffstat (limited to 'crates/ra_assists/src/handlers/add_explicit_type.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/add_explicit_type.rs | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs index 0c7d5e355..ab20c6649 100644 --- a/crates/ra_assists/src/handlers/add_explicit_type.rs +++ b/crates/ra_assists/src/handlers/add_explicit_type.rs | |||
@@ -25,9 +25,8 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio | |||
25 | let stmt = ctx.find_node_at_offset::<LetStmt>()?; | 25 | let stmt = ctx.find_node_at_offset::<LetStmt>()?; |
26 | let module = ctx.sema.scope(stmt.syntax()).module()?; | 26 | let module = ctx.sema.scope(stmt.syntax()).module()?; |
27 | let expr = stmt.initializer()?; | 27 | let expr = stmt.initializer()?; |
28 | let pat = stmt.pat()?; | ||
29 | // Must be a binding | 28 | // Must be a binding |
30 | let pat = match pat { | 29 | let pat = match stmt.pat()? { |
31 | ast::Pat::BindPat(bind_pat) => bind_pat, | 30 | ast::Pat::BindPat(bind_pat) => bind_pat, |
32 | _ => return None, | 31 | _ => return None, |
33 | }; | 32 | }; |
@@ -46,7 +45,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio | |||
46 | // Assist not applicable if the type has already been specified | 45 | // Assist not applicable if the type has already been specified |
47 | // and it has no placeholders | 46 | // and it has no placeholders |
48 | let ascribed_ty = stmt.ascribed_type(); | 47 | let ascribed_ty = stmt.ascribed_type(); |
49 | if let Some(ref ty) = ascribed_ty { | 48 | if let Some(ty) = &ascribed_ty { |
50 | if ty.syntax().descendants().find_map(ast::PlaceholderType::cast).is_none() { | 49 | if ty.syntax().descendants().find_map(ast::PlaceholderType::cast).is_none() { |
51 | return None; | 50 | return None; |
52 | } | 51 | } |
@@ -87,11 +86,7 @@ mod tests { | |||
87 | 86 | ||
88 | #[test] | 87 | #[test] |
89 | fn add_explicit_type_works_for_simple_expr() { | 88 | fn add_explicit_type_works_for_simple_expr() { |
90 | check_assist( | 89 | check_assist(add_explicit_type, "fn f() { let a<|> = 1; }", "fn f() { let a: i32 = 1; }"); |
91 | add_explicit_type, | ||
92 | "fn f() { let a<|> = 1; }", | ||
93 | "fn f() { let a<|>: i32 = 1; }", | ||
94 | ); | ||
95 | } | 90 | } |
96 | 91 | ||
97 | #[test] | 92 | #[test] |
@@ -99,7 +94,7 @@ mod tests { | |||
99 | check_assist( | 94 | check_assist( |
100 | add_explicit_type, | 95 | add_explicit_type, |
101 | "fn f() { let a<|>: _ = 1; }", | 96 | "fn f() { let a<|>: _ = 1; }", |
102 | "fn f() { let a<|>: i32 = 1; }", | 97 | "fn f() { let a: i32 = 1; }", |
103 | ); | 98 | ); |
104 | } | 99 | } |
105 | 100 | ||
@@ -123,7 +118,7 @@ mod tests { | |||
123 | } | 118 | } |
124 | 119 | ||
125 | fn f() { | 120 | fn f() { |
126 | let a<|>: Option<i32> = Option::Some(1); | 121 | let a: Option<i32> = Option::Some(1); |
127 | }"#, | 122 | }"#, |
128 | ); | 123 | ); |
129 | } | 124 | } |
@@ -133,7 +128,7 @@ mod tests { | |||
133 | check_assist( | 128 | check_assist( |
134 | add_explicit_type, | 129 | add_explicit_type, |
135 | r"macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }", | 130 | r"macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }", |
136 | r"macro_rules! v { () => {0u64} } fn f() { let a<|>: u64 = v!(); }", | 131 | r"macro_rules! v { () => {0u64} } fn f() { let a: u64 = v!(); }", |
137 | ); | 132 | ); |
138 | } | 133 | } |
139 | 134 | ||
@@ -141,8 +136,8 @@ mod tests { | |||
141 | fn add_explicit_type_works_for_macro_call_recursive() { | 136 | fn add_explicit_type_works_for_macro_call_recursive() { |
142 | check_assist( | 137 | check_assist( |
143 | add_explicit_type, | 138 | add_explicit_type, |
144 | "macro_rules! u { () => {0u64} } macro_rules! v { () => {u!()} } fn f() { let a<|> = v!(); }", | 139 | r#"macro_rules! u { () => {0u64} } macro_rules! v { () => {u!()} } fn f() { let a<|> = v!(); }"#, |
145 | "macro_rules! u { () => {0u64} } macro_rules! v { () => {u!()} } fn f() { let a<|>: u64 = v!(); }", | 140 | r#"macro_rules! u { () => {0u64} } macro_rules! v { () => {u!()} } fn f() { let a: u64 = v!(); }"#, |
146 | ); | 141 | ); |
147 | } | 142 | } |
148 | 143 | ||
@@ -209,7 +204,7 @@ struct Test<K, T = u8> { | |||
209 | } | 204 | } |
210 | 205 | ||
211 | fn main() { | 206 | fn main() { |
212 | let test<|>: Test<i32> = Test { t: 23, k: 33 }; | 207 | let test: Test<i32> = Test { t: 23, k: 33 }; |
213 | }"#, | 208 | }"#, |
214 | ); | 209 | ); |
215 | } | 210 | } |