Merge pull request #2101 from topecongiro/issue-2099

Format match expr with empty body
This commit is contained in:
Nick Cameron 2017-10-31 08:26:08 +13:00 committed by GitHub
commit 0af8825eb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 18 deletions

View File

@ -1444,15 +1444,10 @@ 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 new_width = context.config.max_width().checked_sub(shape.used_width())?;
let cond_shape = Shape {
width: new_width,
width: context.budget(shape.used_width()),
..shape
};
// 6 = `match `
@ -1485,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()
};
@ -1498,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 {

View File

@ -415,3 +415,13 @@ fn match_with_trailing_spaces() {
None => 1,
}
}
fn issue_2099() {
let a = match x {
};
let b = match x {
};
match x {}
}

View File

@ -456,3 +456,10 @@ fn match_with_trailing_spaces() {
None => 1,
}
}
fn issue_2099() {
let a = match x {};
let b = match x {};
match x {}
}