about summary refs log tree commit diff
path: root/src/utils.rs
diff options
context:
space:
mode:
authorNick Cameron <ncameron@mozilla.com>2018-10-15 14:06:52 +1300
committerNick Cameron <ncameron@mozilla.com>2018-10-15 14:06:52 +1300
commite29fd7bebe0ba302ec8d326a02c86b7fe71c30b4 (patch)
treeca331c293dc780c657dd0cd3ca0fb059be1c4e44 /src/utils.rs
parent2f8c1fea72dee881b602d850422568b1f8c14d12 (diff)
Only put `{` on a newline in a match arm where necessary
Fixes #3005
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs
index 63b238a7817..7d9272fe7e3 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -417,3 +417,35 @@ pub fn starts_with_newline(s: &str) -> bool {
 pub fn first_line_ends_with(s: &str, c: char) -> bool {
     s.lines().next().map_or(false, |l| l.ends_with(c))
 }
+
+// States whether an expression's last line exclusively consists of closing
+// parens, braces, and brackets in its idiomatic formatting.
+pub fn is_block_expr(context: &RewriteContext, expr: &ast::Expr, repr: &str) -> bool {
+    match expr.node {
+        ast::ExprKind::Mac(..)
+        | ast::ExprKind::Call(..)
+        | ast::ExprKind::MethodCall(..)
+        | ast::ExprKind::Array(..)
+        | ast::ExprKind::Struct(..)
+        | ast::ExprKind::While(..)
+        | ast::ExprKind::WhileLet(..)
+        | ast::ExprKind::If(..)
+        | ast::ExprKind::IfLet(..)
+        | ast::ExprKind::Block(..)
+        | ast::ExprKind::Loop(..)
+        | ast::ExprKind::ForLoop(..)
+        | ast::ExprKind::Match(..) => repr.contains('\n'),
+        ast::ExprKind::Paren(ref expr)
+        | ast::ExprKind::Binary(_, _, ref expr)
+        | ast::ExprKind::Index(_, ref expr)
+        | ast::ExprKind::Unary(_, ref expr)
+        | ast::ExprKind::Closure(_, _, _, _, ref expr, _)
+        | ast::ExprKind::Try(ref expr)
+        | ast::ExprKind::Yield(Some(ref expr)) => is_block_expr(context, expr, repr),
+        // This can only be a string lit
+        ast::ExprKind::Lit(_) => {
+            repr.contains('\n') && trimmed_last_line_width(repr) <= context.config.tab_spaces()
+        }
+        _ => false,
+    }
+}