From 792f48c976f006d75229a52441f1a3604c2d0335 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 30 Oct 2017 23:31:08 +0900 Subject: [PATCH 1/3] Add a test for #2099 --- tests/source/match.rs | 10 ++++++++++ tests/target/match.rs | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/tests/source/match.rs b/tests/source/match.rs index 527ceea06ef..48a710e6f06 100644 --- a/tests/source/match.rs +++ b/tests/source/match.rs @@ -415,3 +415,13 @@ fn match_with_trailing_spaces() { None => 1, } } + +fn issue_2099() { + let a = match x { +}; + let b = match x { + + }; + + match x {} +} diff --git a/tests/target/match.rs b/tests/target/match.rs index e42b1e3428b..3d879d18c20 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -456,3 +456,10 @@ fn match_with_trailing_spaces() { None => 1, } } + +fn issue_2099() { + let a = match x {}; + let b = match x {}; + + match x {} +} From e84e01c4428d1a16c04597e963d16bdb6427e9c7 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 30 Oct 2017 23:31:20 +0900 Subject: [PATCH 2/3] Use context.budget() --- src/expr.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 9a020646927..67f8af1abbd 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1450,9 +1450,8 @@ fn rewrite_match( // 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 ` From 2e06dea146eddea8a7fc4ad22f12cfe8fd9e1eed Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 30 Oct 2017 23:36:42 +0900 Subject: [PATCH 3/3] 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 67f8af1abbd..212f05b52b8 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 {