aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-03-27 11:21:57 +0000
committerGitHub <[email protected]>2020-03-27 11:21:57 +0000
commita4901fdcfd5ef056adc3b1999582e6322e911dbd (patch)
tree97c4920c8f89416795311429955d9801799a58bb /crates/ra_syntax/src
parentf9cf86475c042a41f2e3b59baf96b456e0ff521b (diff)
parent91e482b46d43a24cd0a48ea1119b93105140cff2 (diff)
Merge #3742
3742: Replace if with if-let r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/ast/edit.rs11
-rw-r--r--crates/ra_syntax/src/ast/make.rs5
2 files changed, 11 insertions, 5 deletions
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 {
251 #[must_use] 251 #[must_use]
252 pub fn with_use_tree(&self, use_tree: ast::UseTree) -> ast::UseItem { 252 pub fn with_use_tree(&self, use_tree: ast::UseTree) -> ast::UseItem {
253 if let Some(old) = self.use_tree() { 253 if let Some(old) = self.use_tree() {
254 return self.replace_descendants(iter::once((old, use_tree))); 254 return self.replace_descendant(old, use_tree);
255 } 255 }
256 self.clone() 256 self.clone()
257 } 257 }
@@ -283,7 +283,7 @@ impl ast::UseTree {
283 #[must_use] 283 #[must_use]
284 pub fn with_path(&self, path: ast::Path) -> ast::UseTree { 284 pub fn with_path(&self, path: ast::Path) -> ast::UseTree {
285 if let Some(old) = self.path() { 285 if let Some(old) = self.path() {
286 return self.replace_descendants(iter::once((old, path))); 286 return self.replace_descendant(old, path);
287 } 287 }
288 self.clone() 288 self.clone()
289 } 289 }
@@ -291,7 +291,7 @@ impl ast::UseTree {
291 #[must_use] 291 #[must_use]
292 pub fn with_use_tree_list(&self, use_tree_list: ast::UseTreeList) -> ast::UseTree { 292 pub fn with_use_tree_list(&self, use_tree_list: ast::UseTreeList) -> ast::UseTree {
293 if let Some(old) = self.use_tree_list() { 293 if let Some(old) = self.use_tree_list() {
294 return self.replace_descendants(iter::once((old, use_tree_list))); 294 return self.replace_descendant(old, use_tree_list);
295 } 295 }
296 self.clone() 296 self.clone()
297 } 297 }
@@ -466,6 +466,11 @@ pub trait AstNodeEdit: AstNode + Sized {
466 } 466 }
467 467
468 #[must_use] 468 #[must_use]
469 fn replace_descendant<D: AstNode>(&self, old: D, new: D) -> Self {
470 self.replace_descendants(iter::once((old, new)))
471 }
472
473 #[must_use]
469 fn replace_descendants<D: AstNode>( 474 fn replace_descendants<D: AstNode>(
470 &self, 475 &self,
471 replacement_map: impl IntoIterator<Item = (D, D)>, 476 replacement_map: impl IntoIterator<Item = (D, D)>,
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs
index 69bacf224..4621c9888 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::Pat>) -> ast::Condition {
127 match pattern { 127 match pattern {
128 None => ast_from_text(&format!("const _: () = while {} {{}};", expr)), 128 None => ast_from_text(&format!("const _: () = while {} {{}};", expr)),
129 Some(pattern) => { 129 Some(pattern) => {
130 ast_from_text(&format!("const _: () = while {} = {} {{}};", pattern, expr)) 130 ast_from_text(&format!("const _: () = while let {} = {} {{}};", pattern, expr))
131 } 131 }
132 } 132 }
133} 133}
@@ -245,7 +245,8 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt
245 ast_from_text(&format!("fn f() {{ {} }}", text)) 245 ast_from_text(&format!("fn f() {{ {} }}", text))
246} 246}
247pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt { 247pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt {
248 ast_from_text(&format!("fn f() {{ {}; }}", expr)) 248 let semi = if expr.is_block_like() { "" } else { ";" };
249 ast_from_text(&format!("fn f() {{ {}{} (); }}", expr, semi))
249} 250}
250 251
251pub fn token(kind: SyntaxKind) -> SyntaxToken { 252pub fn token(kind: SyntaxKind) -> SyntaxToken {