@@ -1444,15 +1444,10 @@ fn rewrite_match(
1444
1444
span : Span ,
1445
1445
attrs : & [ ast:: Attribute ] ,
1446
1446
) -> Option < String > {
1447
- if arms. is_empty ( ) {
1448
- return None ;
1449
- }
1450
-
1451
1447
// Do not take the rhs overhead from the upper expressions into account
1452
1448
// when rewriting match condition.
1453
- let new_width = context. config . max_width ( ) . checked_sub ( shape. used_width ( ) ) ?;
1454
1449
let cond_shape = Shape {
1455
- width : new_width ,
1450
+ width : context . budget ( shape . used_width ( ) ) ,
1456
1451
..shape
1457
1452
} ;
1458
1453
// 6 = `match `
@@ -1485,9 +1480,12 @@ fn rewrite_match(
1485
1480
} ;
1486
1481
1487
1482
let open_brace_pos = if inner_attrs. is_empty ( ) {
1488
- context
1489
- . codemap
1490
- . span_after ( mk_sp ( cond. span . hi ( ) , arms[ 0 ] . span ( ) . lo ( ) ) , "{" )
1483
+ let hi = if arms. is_empty ( ) {
1484
+ span. hi ( )
1485
+ } else {
1486
+ arms[ 0 ] . span ( ) . lo ( )
1487
+ } ;
1488
+ context. codemap . span_after ( mk_sp ( cond. span . hi ( ) , hi) , "{" )
1491
1489
} else {
1492
1490
inner_attrs[ inner_attrs. len ( ) - 1 ] . span ( ) . hi ( )
1493
1491
} ;
@@ -1498,15 +1496,25 @@ fn rewrite_match(
1498
1496
shape. indent . to_string ( context. config )
1499
1497
} ;
1500
1498
1501
- Some ( format ! (
1502
- "match {}{}{{\n {}{}{}\n {}}}" ,
1503
- cond_str,
1504
- block_sep,
1505
- inner_attrs_str,
1506
- arm_indent_str,
1507
- rewrite_match_arms( context, arms, shape, span, open_brace_pos, ) ?,
1508
- shape. indent. to_string( context. config) ,
1509
- ) )
1499
+ if arms. is_empty ( ) {
1500
+ let snippet = context. snippet ( mk_sp ( open_brace_pos, span. hi ( ) - BytePos ( 1 ) ) ) ;
1501
+ if snippet. trim ( ) . is_empty ( ) {
1502
+ Some ( format ! ( "match {} {{}}" , cond_str) )
1503
+ } else {
1504
+ // Empty match with comments or inner attributes? We are not going to bother, sorry ;)
1505
+ Some ( context. snippet ( span) )
1506
+ }
1507
+ } else {
1508
+ Some ( format ! (
1509
+ "match {}{}{{\n {}{}{}\n {}}}" ,
1510
+ cond_str,
1511
+ block_sep,
1512
+ inner_attrs_str,
1513
+ arm_indent_str,
1514
+ rewrite_match_arms( context, arms, shape, span, open_brace_pos) ?,
1515
+ shape. indent. to_string( context. config) ,
1516
+ ) )
1517
+ }
1510
1518
}
1511
1519
1512
1520
fn arm_comma ( config : & Config , body : & ast:: Expr , is_last : bool ) -> & ' static str {
0 commit comments