mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +00:00
Add tests for LetStmt::set_ty
This commit is contained in:
parent
787ca888e3
commit
df629627c5
1 changed files with 48 additions and 14 deletions
|
@ -663,28 +663,31 @@ impl ast::LetStmt {
|
||||||
|
|
||||||
ted::remove(existing_ty.syntax());
|
ted::remove(existing_ty.syntax());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove any trailing ws
|
||||||
|
if let Some(last) = self.syntax().last_token().filter(|it| it.kind() == WHITESPACE)
|
||||||
|
{
|
||||||
|
last.detach();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Some(new_ty) => {
|
Some(new_ty) => {
|
||||||
if self.colon_token().is_none() {
|
if self.colon_token().is_none() {
|
||||||
let mut to_insert: Vec<SyntaxElement> = vec![];
|
ted::insert_raw(
|
||||||
|
Position::after(
|
||||||
let position = match self.pat() {
|
self.pat().expect("let stmt should have a pattern").syntax(),
|
||||||
Some(pat) => Position::after(pat.syntax()),
|
),
|
||||||
None => {
|
make::token(T![:]),
|
||||||
to_insert.push(make::tokens::single_space().into());
|
);
|
||||||
Position::after(self.let_token().unwrap())
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
to_insert.push(make::token(T![:]).into());
|
|
||||||
|
|
||||||
ted::insert_all_raw(position, to_insert);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(old_ty) = self.ty() {
|
||||||
|
ted::replace(old_ty.syntax(), new_ty.syntax());
|
||||||
|
} else {
|
||||||
ted::insert(Position::after(self.colon_token().unwrap()), new_ty.syntax());
|
ted::insert(Position::after(self.colon_token().unwrap()), new_ty.syntax());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::RecordExprFieldList {
|
impl ast::RecordExprFieldList {
|
||||||
|
@ -1022,6 +1025,37 @@ mod tests {
|
||||||
check("let a @ ", "let a", None);
|
check("let a @ ", "let a", None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_let_stmt_set_ty() {
|
||||||
|
#[track_caller]
|
||||||
|
fn check(before: &str, expected: &str, ty: Option<ast::Type>) {
|
||||||
|
let ty = ty.map(|it| it.clone_for_update());
|
||||||
|
|
||||||
|
let let_stmt = ast_mut_from_text::<ast::LetStmt>(&format!("fn f() {{ {before} }}"));
|
||||||
|
let_stmt.set_ty(ty);
|
||||||
|
|
||||||
|
let after = ast_mut_from_text::<ast::LetStmt>(&format!("fn f() {{ {expected} }}"));
|
||||||
|
assert_eq!(let_stmt.to_string(), after.to_string(), "{let_stmt:#?}\n!=\n{after:#?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// adding
|
||||||
|
check("let a;", "let a: ();", Some(make::ty_tuple([])));
|
||||||
|
// no semicolon due to it being eaten during error recovery
|
||||||
|
check("let a:", "let a: ()", Some(make::ty_tuple([])));
|
||||||
|
|
||||||
|
// replacing
|
||||||
|
check("let a: u8;", "let a: ();", Some(make::ty_tuple([])));
|
||||||
|
check("let a: u8 = 3;", "let a: () = 3;", Some(make::ty_tuple([])));
|
||||||
|
check("let a: = 3;", "let a: () = 3;", Some(make::ty_tuple([])));
|
||||||
|
|
||||||
|
// removing
|
||||||
|
check("let a: u8;", "let a;", None);
|
||||||
|
check("let a:;", "let a;", None);
|
||||||
|
|
||||||
|
check("let a: u8 = 3;", "let a = 3;", None);
|
||||||
|
check("let a: = 3;", "let a = 3;", None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add_variant_to_empty_enum() {
|
fn add_variant_to_empty_enum() {
|
||||||
let variant = make::variant(make::name("Bar"), None).clone_for_update();
|
let variant = make::variant(make::name("Bar"), None).clone_for_update();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue