aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-20 18:16:35 +0000
committerGitHub <[email protected]>2019-11-20 18:16:35 +0000
commit5aec3e4a7b21855fb4688e466d4f5bee9feabe2e (patch)
tree41a304a33955dd12d1f0ec127a80551367c8ffa0
parent7a5fd1f3f3c4a8f64407f9bd08a3d5e7f79417e0 (diff)
parentbcb2ea912bf96f38505c67a0b6896c6a5ac278ed (diff)
Merge #2329
2329: fix 2190; add test for "replace if let with match" r=matklad a=fkohlgrueber Fixes #2190. Check that the expression doesn't contain newlines only for "replace if let with match". For "join lines", enclosing blocks for multiline expressions should be removed. Co-authored-by: Felix Kohlgrüber <[email protected]>
-rw-r--r--crates/ra_assists/src/assists/replace_if_let_with_match.rs32
-rw-r--r--crates/ra_fmt/src/lib.rs3
-rw-r--r--crates/ra_ide_api/src/join_lines.rs28
3 files changed, 58 insertions, 5 deletions
diff --git a/crates/ra_assists/src/assists/replace_if_let_with_match.rs b/crates/ra_assists/src/assists/replace_if_let_with_match.rs
index dff84d865..3272801ff 100644
--- a/crates/ra_assists/src/assists/replace_if_let_with_match.rs
+++ b/crates/ra_assists/src/assists/replace_if_let_with_match.rs
@@ -66,8 +66,8 @@ fn build_match_expr(
66 66
67fn format_arm(block: &ast::BlockExpr) -> String { 67fn format_arm(block: &ast::BlockExpr) -> String {
68 match extract_trivial_expression(block) { 68 match extract_trivial_expression(block) {
69 None => block.syntax().text().to_string(), 69 Some(e) if !e.syntax().text().contains_char('\n') => format!("{},", e.syntax().text()),
70 Some(e) => format!("{},", e.syntax().text()), 70 _ => block.syntax().text().to_string(),
71 } 71 }
72} 72}
73 73
@@ -103,6 +103,34 @@ impl VariantData {
103 } 103 }
104 104
105 #[test] 105 #[test]
106 fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() {
107 check_assist(
108 replace_if_let_with_match,
109 "
110fn foo() {
111 if <|>let VariantData::Struct(..) = a {
112 bar(
113 123
114 )
115 } else {
116 false
117 }
118} ",
119 "
120fn foo() {
121 <|>match a {
122 VariantData::Struct(..) => {
123 bar(
124 123
125 )
126 }
127 _ => false,
128 }
129} ",
130 )
131 }
132
133 #[test]
106 fn replace_if_let_with_match_target() { 134 fn replace_if_let_with_match_target() {
107 check_assist_target( 135 check_assist_target(
108 replace_if_let_with_match, 136 replace_if_let_with_match,
diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs
index a30ed4cbb..10f592257 100644
--- a/crates/ra_fmt/src/lib.rs
+++ b/crates/ra_fmt/src/lib.rs
@@ -38,9 +38,6 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
38pub fn extract_trivial_expression(expr: &ast::BlockExpr) -> Option<ast::Expr> { 38pub fn extract_trivial_expression(expr: &ast::BlockExpr) -> Option<ast::Expr> {
39 let block = expr.block()?; 39 let block = expr.block()?;
40 let expr = block.expr()?; 40 let expr = block.expr()?;
41 if expr.syntax().text().contains_char('\n') {
42 return None;
43 }
44 let non_trivial_children = block.syntax().children().filter(|it| match it.kind() { 41 let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
45 WHITESPACE | T!['{'] | T!['}'] => false, 42 WHITESPACE | T!['{'] | T!['}'] => false,
46 _ => it != expr.syntax(), 43 _ => it != expr.syntax(),
diff --git a/crates/ra_ide_api/src/join_lines.rs b/crates/ra_ide_api/src/join_lines.rs
index 6f71b27db..7deeb3494 100644
--- a/crates/ra_ide_api/src/join_lines.rs
+++ b/crates/ra_ide_api/src/join_lines.rs
@@ -244,6 +244,34 @@ fn foo(e: Result<U, V>) {
244 } 244 }
245 245
246 #[test] 246 #[test]
247 fn join_lines_multiline_in_block() {
248 check_join_lines(
249 r"
250fn foo() {
251 match ty {
252 <|> Some(ty) => {
253 match ty {
254 _ => false,
255 }
256 }
257 _ => true,
258 }
259}
260",
261 r"
262fn foo() {
263 match ty {
264 <|> Some(ty) => match ty {
265 _ => false,
266 },
267 _ => true,
268 }
269}
270",
271 );
272 }
273
274 #[test]
247 fn join_lines_keeps_comma_for_block_in_match_arm() { 275 fn join_lines_keeps_comma_for_block_in_match_arm() {
248 // We already have a comma 276 // We already have a comma
249 check_join_lines( 277 check_join_lines(