7972: fix: add semicolon after type ascription r=matklad a=conradludgate

Fixes #7971

Co-authored-by: Conrad Ludgate <conradludgate@gmail.com>
This commit is contained in:
bors[bot] 2021-03-11 10:41:00 +00:00 committed by GitHub
commit db6364fecc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -56,13 +56,20 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<(
if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() {
if let_stmt.colon_token().is_none() {
let type_pos = let_stmt.pat()?.syntax().last_token()?.text_range().end();
let semi_pos = let_stmt.syntax().last_token()?.text_range().end();
acc.add(
AssistId("add_type_ascription", AssistKind::RefactorRewrite),
"Add `: _` before assignment operator",
ident.text_range(),
|builder| match ctx.config.snippet_cap {
Some(cap) => builder.insert_snippet(cap, type_pos, ": ${0:_}"),
None => builder.insert(type_pos, ": _"),
|builder| {
if let_stmt.semicolon_token().is_none() {
builder.insert(semi_pos, ";");
}
match ctx.config.snippet_cap {
Some(cap) => builder.insert_snippet(cap, type_pos, ": ${0:_}"),
None => builder.insert(type_pos, ": _"),
}
},
)?
} else {
@ -265,4 +272,24 @@ fn main() {
"#,
);
}
#[test]
fn add_type_ascription_append_semicolon() {
check_assist_by_label(
add_turbo_fish,
r#"
fn make<T>() -> T {}
fn main() {
let x = make$0()
}
"#,
r#"
fn make<T>() -> T {}
fn main() {
let x: ${0:_} = make();
}
"#,
"Add `: _` before assignment operator",
);
}
}