aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-09 14:58:22 +0100
committerGitHub <[email protected]>2020-05-09 14:58:22 +0100
commit77d2eaba1679832f4c00241645712460351eddb4 (patch)
tree4724422ed5f7242cd23987f30133dc211895154b /crates
parentc5c07c2a0a1b4809c5aacef3c8e90034d00c3b03 (diff)
parente201e7834d623f954e06925b9de0f14cd5ff90c1 (diff)
Merge #4389
4389: Ascribe more correct types r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/handlers/add_explicit_type.rs19
1 files changed, 10 insertions, 9 deletions
diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs
index 55409e501..146cc75df 100644
--- a/crates/ra_assists/src/handlers/add_explicit_type.rs
+++ b/crates/ra_assists/src/handlers/add_explicit_type.rs
@@ -23,6 +23,7 @@ use crate::{AssistContext, AssistId, Assists};
23// ``` 23// ```
24pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 24pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
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 expr = stmt.initializer()?; 27 let expr = stmt.initializer()?;
27 let pat = stmt.pat()?; 28 let pat = stmt.pat()?;
28 // Must be a binding 29 // Must be a binding
@@ -57,17 +58,17 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
57 return None; 58 return None;
58 } 59 }
59 60
60 let db = ctx.db; 61 let inferred_type = ty.display_source_code(ctx.db, module.into()).ok()?;
61 let new_type_string = ty.display_truncated(db, None).to_string();
62 acc.add( 62 acc.add(
63 AssistId("add_explicit_type"), 63 AssistId("add_explicit_type"),
64 format!("Insert explicit type '{}'", new_type_string), 64 format!("Insert explicit type '{}'", inferred_type),
65 pat_range, 65 pat_range,
66 |edit| { 66 |builder| match ascribed_ty {
67 if let Some(ascribed_ty) = ascribed_ty { 67 Some(ascribed_ty) => {
68 edit.replace(ascribed_ty.syntax().text_range(), new_type_string); 68 builder.replace(ascribed_ty.syntax().text_range(), inferred_type);
69 } else { 69 }
70 edit.insert(name_range.end(), format!(": {}", new_type_string)); 70 None => {
71 builder.insert(name_range.end(), format!(": {}", inferred_type));
71 } 72 }
72 }, 73 },
73 ) 74 )
@@ -208,7 +209,7 @@ struct Test<K, T = u8> {
208} 209}
209 210
210fn main() { 211fn main() {
211 let test<|>: Test<i32> = Test { t: 23, k: 33 }; 212 let test<|>: Test<i32, u8> = Test { t: 23, k: 33 };
212}"#, 213}"#,
213 ); 214 );
214 } 215 }