mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
Ascribe more correct types
This commit is contained in:
parent
c5c07c2a0a
commit
e201e7834d
@ -23,6 +23,7 @@ use crate::{AssistContext, AssistId, Assists};
|
|||||||
// ```
|
// ```
|
||||||
pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||||
let stmt = ctx.find_node_at_offset::<LetStmt>()?;
|
let stmt = ctx.find_node_at_offset::<LetStmt>()?;
|
||||||
|
let module = ctx.sema.scope(stmt.syntax()).module()?;
|
||||||
let expr = stmt.initializer()?;
|
let expr = stmt.initializer()?;
|
||||||
let pat = stmt.pat()?;
|
let pat = stmt.pat()?;
|
||||||
// Must be a binding
|
// Must be a binding
|
||||||
@ -57,17 +58,17 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let db = ctx.db;
|
let inferred_type = ty.display_source_code(ctx.db, module.into()).ok()?;
|
||||||
let new_type_string = ty.display_truncated(db, None).to_string();
|
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("add_explicit_type"),
|
AssistId("add_explicit_type"),
|
||||||
format!("Insert explicit type '{}'", new_type_string),
|
format!("Insert explicit type '{}'", inferred_type),
|
||||||
pat_range,
|
pat_range,
|
||||||
|edit| {
|
|builder| match ascribed_ty {
|
||||||
if let Some(ascribed_ty) = ascribed_ty {
|
Some(ascribed_ty) => {
|
||||||
edit.replace(ascribed_ty.syntax().text_range(), new_type_string);
|
builder.replace(ascribed_ty.syntax().text_range(), inferred_type);
|
||||||
} else {
|
}
|
||||||
edit.insert(name_range.end(), format!(": {}", new_type_string));
|
None => {
|
||||||
|
builder.insert(name_range.end(), format!(": {}", inferred_type));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -208,7 +209,7 @@ struct Test<K, T = u8> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let test<|>: Test<i32> = Test { t: 23, k: 33 };
|
let test<|>: Test<i32, u8> = Test { t: 23, k: 33 };
|
||||||
}"#,
|
}"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user