diff --git a/src/expr.rs b/src/expr.rs index 95da7d3659f1..2c57bb5b64de 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -117,7 +117,7 @@ impl Rewrite for ast::Expr { offset) } ast::Expr_::ExprMatch(ref cond, ref arms, _) => { - rewrite_match(context, cond, arms, width, offset) + rewrite_match(context, cond, arms, width, offset, self.span) } ast::Expr_::ExprPath(ref qself, ref path) => { rewrite_path(context, qself.as_ref(), path, width, offset) @@ -624,7 +624,8 @@ fn rewrite_match(context: &RewriteContext, cond: &ast::Expr, arms: &[ast::Arm], width: usize, - offset: Indent) + offset: Indent, + span: Span) -> Option { if arms.is_empty() { return None; @@ -666,7 +667,8 @@ fn rewrite_match(context: &RewriteContext, if !missed_str.is_empty() { result.push('\n'); result.push_str(&arm_indent_str); - result.push_str(missed_str); + result.push_str(&rewrite_comment(&missed_str, false, + width, arm_indent, context.config)); } result.push('\n'); result.push_str(&arm_indent_str); @@ -682,11 +684,23 @@ fn rewrite_match(context: &RewriteContext, result.push_str(&snippet); } } - - // We'll miss any comments etc. between the last arm and the end of the - // match expression, but meh. + let last_comment = context.snippet(mk_sp(arm_end_pos(&arms[arms.len() - 1]), span.hi)); + let last_comment = match last_comment.find_uncommented(",") { + Some(n) => &last_comment[n+1..], + None => &last_comment[..], + }; + let last_comment = match last_comment.find_uncommented("}") { + Some(n) => &last_comment[..n-1], + None => &last_comment[..], + }; + let last_comment = last_comment.trim(); result.push('\n'); + if last_comment.len() > 0 { + result.push_str(&arm_indent_str); + result.push_str(&rewrite_comment(&last_comment, false, width, arm_indent, context.config)); + result.push('\n'); + } result.push_str(&(context.block_indent + context.overflow_indent).to_string(context.config)); result.push('}'); Some(result)