From 91e482b46d43a24cd0a48ea1119b93105140cff2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 27 Mar 2020 12:12:17 +0100 Subject: Replace if with if-let --- crates/ra_syntax/src/ast/edit.rs | 11 ++++++++--- crates/ra_syntax/src/ast/make.rs | 5 +++-- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index f74c9f9c6..bdaecdc43 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -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(&self, old: D, new: D) -> Self { + self.replace_descendants(iter::once((old, new))) + } + #[must_use] fn replace_descendants( &self, diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 6aee39203..c818bba55 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -127,7 +127,7 @@ pub fn condition(expr: ast::Expr, pattern: Option) -> 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::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 { -- cgit v1.2.3