Skip to content

Commit 0af8825

Browse files
authored
Merge pull request #2101 from topecongiro/issue-2099
Format match expr with empty body
2 parents 48fb113 + 2e06dea commit 0af8825

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

src/expr.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,15 +1444,10 @@ 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.
1453-
let new_width = context.config.max_width().checked_sub(shape.used_width())?;
14541449
let cond_shape = Shape {
1455-
width: new_width,
1450+
width: context.budget(shape.used_width()),
14561451
..shape
14571452
};
14581453
// 6 = `match `
@@ -1485,9 +1480,12 @@ fn rewrite_match(
14851480
};
14861481

14871482
let open_brace_pos = if inner_attrs.is_empty() {
1488-
context
1489-
.codemap
1490-
.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), "{")
14911489
} else {
14921490
inner_attrs[inner_attrs.len() - 1].span().hi()
14931491
};
@@ -1498,15 +1496,25 @@ fn rewrite_match(
14981496
shape.indent.to_string(context.config)
14991497
};
15001498

1501-
Some(format!(
1502-
"match {}{}{{\n{}{}{}\n{}}}",
1503-
cond_str,
1504-
block_sep,
1505-
inner_attrs_str,
1506-
arm_indent_str,
1507-
rewrite_match_arms(context, arms, shape, span, open_brace_pos,)?,
1508-
shape.indent.to_string(context.config),
1509-
))
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+
}
15101518
}
15111519

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

tests/source/match.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,13 @@ fn match_with_trailing_spaces() {
415415
None => 1,
416416
}
417417
}
418+
419+
fn issue_2099() {
420+
let a = match x {
421+
};
422+
let b = match x {
423+
424+
};
425+
426+
match x {}
427+
}

tests/target/match.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,10 @@ fn match_with_trailing_spaces() {
456456
None => 1,
457457
}
458458
}
459+
460+
fn issue_2099() {
461+
let a = match x {};
462+
let b = match x {};
463+
464+
match x {}
465+
}

0 commit comments

Comments
 (0)