diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-05-19 19:30:36 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-05-19 19:30:36 +0100 |
commit | 2d76b176c0b0f86648a038a5e4ca84fb04e809dc (patch) | |
tree | 72009d761e24e5807e3ffc0b1f49509301ed144f | |
parent | c7196620abd5e9bab4fbd53388da361f0f6987a1 (diff) | |
parent | 5c5fedb9454b4fcb237728da7c9e29e981fcdc3a (diff) |
Merge #8884
8884: fix: add_explicit_type produces invalid code on `@` patterns r=Veykril a=iDawer
In
```rust
let name @ () = ();
```
an explicit type should be inserted after the pattern, not just after the name.
`let` statement defined as `LetStmt = Attr* 'let' Pat (':' Type)? '=' initializer:Expr ';'`
Co-authored-by: Dawer <[email protected]>
-rw-r--r-- | crates/ide_assists/src/handlers/add_explicit_type.rs | 25 |
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 @@ | |||
1 | use hir::HirDisplay; | 1 | use hir::HirDisplay; |
2 | use syntax::{ | 2 | use 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#" | ||
253 | fn main() { | ||
254 | let $0test @ () = (); | ||
255 | } | ||
256 | "#, | ||
257 | r#" | ||
258 | fn main() { | ||
259 | let test @ (): () = (); | ||
260 | } | ||
261 | "#, | ||
262 | ); | ||
263 | } | ||
249 | } | 264 | } |