From 2e06dea146eddea8a7fc4ad22f12cfe8fd9e1eed Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 30 Oct 2017 23:36:42 +0900 Subject: [PATCH] Format match expr with empty body --- src/expr.rs | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 67f8af1abbd1..212f05b52b82 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1444,10 +1444,6 @@ fn rewrite_match( span: Span, attrs: &[ast::Attribute], ) -> Option { - if arms.is_empty() { - return None; - } - // Do not take the rhs overhead from the upper expressions into account // when rewriting match condition. let cond_shape = Shape { @@ -1484,9 +1480,12 @@ fn rewrite_match( }; let open_brace_pos = if inner_attrs.is_empty() { - context - .codemap - .span_after(mk_sp(cond.span.hi(), arms[0].span().lo()), "{") + let hi = if arms.is_empty() { + span.hi() + } else { + arms[0].span().lo() + }; + context.codemap.span_after(mk_sp(cond.span.hi(), hi), "{") } else { inner_attrs[inner_attrs.len() - 1].span().hi() }; @@ -1497,15 +1496,25 @@ fn rewrite_match( shape.indent.to_string(context.config) }; - Some(format!( - "match {}{}{{\n{}{}{}\n{}}}", - cond_str, - block_sep, - inner_attrs_str, - arm_indent_str, - rewrite_match_arms(context, arms, shape, span, open_brace_pos,)?, - shape.indent.to_string(context.config), - )) + if arms.is_empty() { + let snippet = context.snippet(mk_sp(open_brace_pos, span.hi() - BytePos(1))); + if snippet.trim().is_empty() { + Some(format!("match {} {{}}", cond_str)) + } else { + // Empty match with comments or inner attributes? We are not going to bother, sorry ;) + Some(context.snippet(span)) + } + } else { + Some(format!( + "match {}{}{{\n{}{}{}\n{}}}", + cond_str, + block_sep, + inner_attrs_str, + arm_indent_str, + rewrite_match_arms(context, arms, shape, span, open_brace_pos)?, + shape.indent.to_string(context.config), + )) + } } fn arm_comma(config: &Config, body: &ast::Expr, is_last: bool) -> &'static str {