Skip to content

Commit 2e06dea

Browse files
committed
Format match expr with empty body
1 parent e84e01c commit 2e06dea

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/expr.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,10 +1444,6 @@ fn rewrite_match(
14441444
span: Span,
14451445
attrs: &[ast::Attribute],
14461446
) -> Option<String> {
1447-
if arms.is_empty() {
1448-
return None;
1449-
}
1450-
14511447
// Do not take the rhs overhead from the upper expressions into account
14521448
// when rewriting match condition.
14531449
let cond_shape = Shape {
@@ -1484,9 +1480,12 @@ fn rewrite_match(
14841480
};
14851481

14861482
let open_brace_pos = if inner_attrs.is_empty() {
1487-
context
1488-
.codemap
1489-
.span_after(mk_sp(cond.span.hi(), arms[0].span().lo()), "{")
1483+
let hi = if arms.is_empty() {
1484+
span.hi()
1485+
} else {
1486+
arms[0].span().lo()
1487+
};
1488+
context.codemap.span_after(mk_sp(cond.span.hi(), hi), "{")
14901489
} else {
14911490
inner_attrs[inner_attrs.len() - 1].span().hi()
14921491
};
@@ -1497,15 +1496,25 @@ fn rewrite_match(
14971496
shape.indent.to_string(context.config)
14981497
};
14991498

1500-
Some(format!(
1501-
"match {}{}{{\n{}{}{}\n{}}}",
1502-
cond_str,
1503-
block_sep,
1504-
inner_attrs_str,
1505-
arm_indent_str,
1506-
rewrite_match_arms(context, arms, shape, span, open_brace_pos,)?,
1507-
shape.indent.to_string(context.config),
1508-
))
1499+
if arms.is_empty() {
1500+
let snippet = context.snippet(mk_sp(open_brace_pos, span.hi() - BytePos(1)));
1501+
if snippet.trim().is_empty() {
1502+
Some(format!("match {} {{}}", cond_str))
1503+
} else {
1504+
// Empty match with comments or inner attributes? We are not going to bother, sorry ;)
1505+
Some(context.snippet(span))
1506+
}
1507+
} else {
1508+
Some(format!(
1509+
"match {}{}{{\n{}{}{}\n{}}}",
1510+
cond_str,
1511+
block_sep,
1512+
inner_attrs_str,
1513+
arm_indent_str,
1514+
rewrite_match_arms(context, arms, shape, span, open_brace_pos)?,
1515+
shape.indent.to_string(context.config),
1516+
))
1517+
}
15091518
}
15101519

15111520
fn arm_comma(config: &Config, body: &ast::Expr, is_last: bool) -> &'static str {

0 commit comments

Comments
 (0)