Format match expr with empty body
@@ -1444,10 +1444,6 @@ fn rewrite_match(
span: Span,
attrs: &[ast::Attribute],
) -> Option<String> {
- 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,16 +1496,26 @@ fn rewrite_match(
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,)?,
+ 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 {
if is_last && config.trailing_comma() == SeparatorTactic::Never {
