@@ -326,6 +326,12 @@ fn rewrite_match_body(
326326 arrow_span : Span ,
327327 is_last : bool ,
328328) -> Option < String > {
329+ let was_block = if let ast:: ExprKind :: Block ( ..) = body. kind {
330+ true
331+ } else {
332+ false
333+ } ;
334+
329335 let ( extend, body) = flatten_arm_body (
330336 context,
331337 body,
@@ -368,14 +374,17 @@ fn rewrite_match_body(
368374 let comment_str = arrow_snippet[ arrow_index + 2 ..] . trim ( ) ;
369375 if comment_str. is_empty ( ) {
370376 String :: new ( )
377+ } else if ( !was_block && !is_block) || is_empty_block {
378+ let indent = shape. indent . block_indent ( context. config ) ;
379+ let shape = Shape :: indented ( indent, & context. config ) ;
380+ rewrite_comment ( comment_str, false , shape, & context. config ) ?
371381 } else {
372382 rewrite_comment ( comment_str, false , shape, & context. config ) ?
373383 }
374384 } ;
375385
376386 let combine_next_line_body = |body_str : & str | {
377387 let nested_indent_str = next_line_indent. to_string_with_newline ( context. config ) ;
378-
379388 if is_block {
380389 let mut result = pats_str. to_owned ( ) ;
381390 result. push_str ( " =>" ) ;
@@ -413,19 +422,26 @@ fn rewrite_match_body(
413422 let block_sep = match context. config . control_brace_style ( ) {
414423 ControlBraceStyle :: AlwaysNextLine => format ! ( "{}{}" , alt_block_sep, body_prefix) ,
415424 _ if body_prefix. is_empty ( ) => "" . to_owned ( ) ,
416- _ if forbid_same_line || !arrow_comment. is_empty ( ) => {
425+ _ if forbid_same_line || ( !arrow_comment. is_empty ( ) && was_block ) => {
417426 format ! ( "{}{}" , alt_block_sep, body_prefix)
418427 }
419428 _ => format ! ( " {}" , body_prefix) ,
420429 } + & nested_indent_str;
421430
431+ // if match arm was a block consisting of one expression,
432+ // and it was flattened, we need to retain comment before
433+ // the arm body block.
422434 let mut result = pats_str. to_owned ( ) ;
423435 result. push_str ( " =>" ) ;
424- if !arrow_comment. is_empty ( ) {
436+ if !arrow_comment. is_empty ( ) && was_block {
425437 result. push_str ( & indent_str) ;
426438 result. push_str ( & arrow_comment) ;
427439 }
428440 result. push_str ( & block_sep) ;
441+ if !arrow_comment. is_empty ( ) && !was_block {
442+ result. push_str ( & arrow_comment) ;
443+ result. push_str ( & nested_indent_str) ;
444+ }
429445 result. push_str ( & body_str) ;
430446 result. push_str ( & body_suffix) ;
431447 Some ( result)
0 commit comments