@@ -326,6 +326,12 @@ fn rewrite_match_body(
326
326
arrow_span : Span ,
327
327
is_last : bool ,
328
328
) -> Option < String > {
329
+ let was_block = if let ast:: ExprKind :: Block ( ..) = body. kind {
330
+ true
331
+ } else {
332
+ false
333
+ } ;
334
+
329
335
let ( extend, body) = flatten_arm_body (
330
336
context,
331
337
body,
@@ -368,14 +374,17 @@ fn rewrite_match_body(
368
374
let comment_str = arrow_snippet[ arrow_index + 2 ..] . trim ( ) ;
369
375
if comment_str. is_empty ( ) {
370
376
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 ) ?
371
381
} else {
372
382
rewrite_comment ( comment_str, false , shape, & context. config ) ?
373
383
}
374
384
} ;
375
385
376
386
let combine_next_line_body = |body_str : & str | {
377
387
let nested_indent_str = next_line_indent. to_string_with_newline ( context. config ) ;
378
-
379
388
if is_block {
380
389
let mut result = pats_str. to_owned ( ) ;
381
390
result. push_str ( " =>" ) ;
@@ -413,19 +422,26 @@ fn rewrite_match_body(
413
422
let block_sep = match context. config . control_brace_style ( ) {
414
423
ControlBraceStyle :: AlwaysNextLine => format ! ( "{}{}" , alt_block_sep, body_prefix) ,
415
424
_ 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 ) => {
417
426
format ! ( "{}{}" , alt_block_sep, body_prefix)
418
427
}
419
428
_ => format ! ( " {}" , body_prefix) ,
420
429
} + & nested_indent_str;
421
430
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.
422
434
let mut result = pats_str. to_owned ( ) ;
423
435
result. push_str ( " =>" ) ;
424
- if !arrow_comment. is_empty ( ) {
436
+ if !arrow_comment. is_empty ( ) && was_block {
425
437
result. push_str ( & indent_str) ;
426
438
result. push_str ( & arrow_comment) ;
427
439
}
428
440
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
+ }
429
445
result. push_str ( & body_str) ;
430
446
result. push_str ( & body_suffix) ;
431
447
Some ( result)
0 commit comments