mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Replace if with if-let
This commit is contained in:
parent
cbb53cf55c
commit
91e482b46d
6 changed files with 170 additions and 5 deletions
|
@ -251,7 +251,7 @@ impl ast::UseItem {
|
|||
#[must_use]
|
||||
pub fn with_use_tree(&self, use_tree: ast::UseTree) -> ast::UseItem {
|
||||
if let Some(old) = self.use_tree() {
|
||||
return self.replace_descendants(iter::once((old, use_tree)));
|
||||
return self.replace_descendant(old, use_tree);
|
||||
}
|
||||
self.clone()
|
||||
}
|
||||
|
@ -283,7 +283,7 @@ impl ast::UseTree {
|
|||
#[must_use]
|
||||
pub fn with_path(&self, path: ast::Path) -> ast::UseTree {
|
||||
if let Some(old) = self.path() {
|
||||
return self.replace_descendants(iter::once((old, path)));
|
||||
return self.replace_descendant(old, path);
|
||||
}
|
||||
self.clone()
|
||||
}
|
||||
|
@ -291,7 +291,7 @@ impl ast::UseTree {
|
|||
#[must_use]
|
||||
pub fn with_use_tree_list(&self, use_tree_list: ast::UseTreeList) -> ast::UseTree {
|
||||
if let Some(old) = self.use_tree_list() {
|
||||
return self.replace_descendants(iter::once((old, use_tree_list)));
|
||||
return self.replace_descendant(old, use_tree_list);
|
||||
}
|
||||
self.clone()
|
||||
}
|
||||
|
@ -465,6 +465,11 @@ pub trait AstNodeEdit: AstNode + Sized {
|
|||
Self::cast(new_syntax).unwrap()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
fn replace_descendant<D: AstNode>(&self, old: D, new: D) -> Self {
|
||||
self.replace_descendants(iter::once((old, new)))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
fn replace_descendants<D: AstNode>(
|
||||
&self,
|
||||
|
|
|
@ -127,7 +127,7 @@ pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition {
|
|||
match pattern {
|
||||
None => ast_from_text(&format!("const _: () = while {} {{}};", expr)),
|
||||
Some(pattern) => {
|
||||
ast_from_text(&format!("const _: () = while {} = {} {{}};", pattern, expr))
|
||||
ast_from_text(&format!("const _: () = while let {} = {} {{}};", pattern, expr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -245,7 +245,8 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt
|
|||
ast_from_text(&format!("fn f() {{ {} }}", text))
|
||||
}
|
||||
pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt {
|
||||
ast_from_text(&format!("fn f() {{ {}; }}", expr))
|
||||
let semi = if expr.is_block_like() { "" } else { ";" };
|
||||
ast_from_text(&format!("fn f() {{ {}{} (); }}", expr, semi))
|
||||
}
|
||||
|
||||
pub fn token(kind: SyntaxKind) -> SyntaxToken {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue