@@ -219,7 +219,8 @@ fn rewrite_closure(capture: ast::CaptureClause,
219
219
220
220
// 1 = the separating space between arguments and the body.
221
221
let extra_offset = extra_offset ( & prefix, offset) + 1 ;
222
- let rewrite = inner_expr. rewrite ( context, width - extra_offset, offset + extra_offset) ;
222
+ let budget = try_opt ! ( width. checked_sub( extra_offset) ) ;
223
+ let rewrite = inner_expr. rewrite ( context, budget, offset + extra_offset) ;
223
224
224
225
// Checks if rewrite succeeded and fits on a single line.
225
226
let accept_rewrite = rewrite. as_ref ( ) . map ( |result| !result. contains ( '\n' ) ) . unwrap_or ( false ) ;
@@ -263,7 +264,8 @@ impl Rewrite for ast::Block {
263
264
264
265
if !trimmed. is_empty ( ) {
265
266
// 9 = "unsafe {".len(), 7 = "unsafe ".len()
266
- format ! ( "unsafe {} " , rewrite_comment( trimmed, true , width - 9 , offset + 7 ) )
267
+ let budget = try_opt ! ( width. checked_sub( 9 ) ) ;
268
+ format ! ( "unsafe {} " , rewrite_comment( trimmed, true , budget, offset + 7 ) )
267
269
} else {
268
270
"unsafe " . to_owned ( )
269
271
}
@@ -357,7 +359,7 @@ impl<'a> Rewrite for Loop<'a> {
357
359
fn rewrite ( & self , context : & RewriteContext , width : usize , offset : usize ) -> Option < String > {
358
360
let label_string = rewrite_label ( self . label ) ;
359
361
// 2 = " {".len()
360
- let inner_width = width - self . keyword . len ( ) - 2 - label_string. len ( ) ;
362
+ let inner_width = try_opt ! ( width. checked_sub ( self . keyword. len( ) + 2 + label_string. len( ) ) ) ;
361
363
let inner_offset = offset + self . keyword . len ( ) + label_string. len ( ) ;
362
364
363
365
let pat_expr_string = match self . cond {
@@ -393,14 +395,17 @@ fn rewrite_range(context: &RewriteContext,
393
395
offset : usize )
394
396
-> Option < String > {
395
397
let left_string = match left {
396
- Some ( expr) => try_opt ! ( expr. rewrite( context, width - 2 , offset) ) ,
398
+ Some ( expr) => {
399
+ // 2 = ..
400
+ let max_width = try_opt ! ( width. checked_sub( 2 ) ) ;
401
+ try_opt ! ( expr. rewrite( context, max_width, offset) )
402
+ }
397
403
None => String :: new ( ) ,
398
404
} ;
399
405
400
406
let right_string = match right {
401
407
Some ( expr) => {
402
- // 2 = ..
403
- let max_width = ( width - 2 ) . checked_sub ( left_string. len ( ) ) . unwrap_or ( 0 ) ;
408
+ let max_width = try_opt ! ( width. checked_sub( left_string. len( ) + 2 ) ) ;
404
409
try_opt ! ( expr. rewrite( context, max_width, offset + 2 + left_string. len( ) ) )
405
410
}
406
411
None => String :: new ( ) ,
@@ -532,7 +537,8 @@ fn rewrite_match(context: &RewriteContext,
532
537
}
533
538
534
539
// `match `cond` {`
535
- let cond_str = try_opt ! ( cond. rewrite( context, width - 8 , offset + 6 ) ) ;
540
+ let cond_budget = try_opt ! ( width. checked_sub( 8 ) ) ;
541
+ let cond_str = try_opt ! ( cond. rewrite( context, cond_budget, offset + 6 ) ) ;
536
542
let mut result = format ! ( "match {} {{" , cond_str) ;
537
543
538
544
let block_indent = context. block_indent ;
@@ -632,17 +638,20 @@ impl Rewrite for ast::Arm {
632
638
} ;
633
639
634
640
// Patterns
635
- let pat_strs = try_opt ! ( pats. iter( ) . map( |p| p. rewrite( context,
636
- // 5 = ` => {`
637
- width - 5 ,
638
- offset + context. config. tab_spaces) )
641
+ // 5 = ` => {`
642
+ let pat_budget = try_opt ! ( width. checked_sub( 5 ) ) ;
643
+ let pat_strs = try_opt ! ( pats. iter( ) . map( |p| {
644
+ p. rewrite( context,
645
+ pat_budget,
646
+ offset + context. config. tab_spaces)
647
+ } )
639
648
. collect:: <Option <Vec <_>>>( ) ) ;
640
649
641
650
let mut total_width = pat_strs. iter ( ) . fold ( 0 , |a, p| a + p. len ( ) ) ;
642
651
// Add ` | `.len().
643
652
total_width += ( pat_strs. len ( ) - 1 ) * 3 ;
644
653
645
- let mut vertical = total_width > width - 5 || pat_strs. iter ( ) . any ( |p| p. contains ( '\n' ) ) ;
654
+ let mut vertical = total_width > pat_budget || pat_strs. iter ( ) . any ( |p| p. contains ( '\n' ) ) ;
646
655
if !vertical {
647
656
// If the patterns were previously stacked, keep them stacked.
648
657
// FIXME should be an option.
@@ -710,9 +719,8 @@ impl Rewrite for ast::Arm {
710
719
return None ;
711
720
}
712
721
713
- let body_str = try_opt ! ( body. rewrite( context,
714
- width - context. config. tab_spaces,
715
- nested_indent) ) ;
722
+ let body_budget = try_opt ! ( width. checked_sub( context. config. tab_spaces) ) ;
723
+ let body_str = try_opt ! ( body. rewrite( context, body_budget, nested_indent) ) ;
716
724
Some ( format ! ( "{}{} =>\n {}{}," ,
717
725
attr_str. trim_left( ) ,
718
726
pats_str,
@@ -775,9 +783,8 @@ fn rewrite_pat_expr(context: &RewriteContext,
775
783
let pat_offset = offset + matcher. len ( ) ;
776
784
let mut result = match pat {
777
785
Some ( pat) => {
778
- let pat_string = try_opt ! ( pat. rewrite( context,
779
- width - connector. len( ) - matcher. len( ) ,
780
- pat_offset) ) ;
786
+ let pat_budget = try_opt ! ( width. checked_sub( connector. len( ) + matcher. len( ) ) ) ;
787
+ let pat_string = try_opt ! ( pat. rewrite( context, pat_budget, pat_offset) ) ;
781
788
format ! ( "{}{}{}" , matcher, pat_string, connector)
782
789
}
783
790
None => String :: new ( ) ,
@@ -930,7 +937,8 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
930
937
}
931
938
932
939
// 2 = " {".len()
933
- let path_str = try_opt ! ( path. rewrite( context, width - 2 , offset) ) ;
940
+ let path_budget = try_opt ! ( width. checked_sub( 2 ) ) ;
941
+ let path_str = try_opt ! ( path. rewrite( context, path_budget, offset) ) ;
934
942
935
943
// Foo { a: Foo } - indent is +3, width is -5.
936
944
let h_budget = try_opt ! ( width. checked_sub( path_str. len( ) + 5 ) ) ;
@@ -1041,7 +1049,8 @@ fn rewrite_tuple_lit(context: &RewriteContext,
1041
1049
// In case of length 1, need a trailing comma
1042
1050
if items. len ( ) == 1 {
1043
1051
// 3 = "(" + ",)"
1044
- return items[ 0 ] . rewrite ( context, width - 3 , indent) . map ( |s| format ! ( "({},)" , s) ) ;
1052
+ let budget = try_opt ! ( width. checked_sub( 3 ) ) ;
1053
+ return items[ 0 ] . rewrite ( context, budget, indent) . map ( |s| format ! ( "({},)" , s) ) ;
1045
1054
}
1046
1055
1047
1056
let items = itemize_list ( context. codemap ,
@@ -1057,7 +1066,8 @@ fn rewrite_tuple_lit(context: &RewriteContext,
1057
1066
span. lo + BytePos ( 1 ) , // Remove parens
1058
1067
span. hi - BytePos ( 1 ) ) ;
1059
1068
1060
- let fmt = ListFormatting :: for_fn ( width - 2 , indent) ;
1069
+ let budget = try_opt ! ( width. checked_sub( 2 ) ) ;
1070
+ let fmt = ListFormatting :: for_fn ( budget, indent) ;
1061
1071
1062
1072
Some ( format ! ( "({})" , write_list( & items. collect:: <Vec <_>>( ) , & fmt) ) )
1063
1073
}
@@ -1134,11 +1144,10 @@ fn rewrite_unary_op(context: &RewriteContext,
1134
1144
ast:: UnOp :: UnNot => "!" ,
1135
1145
ast:: UnOp :: UnNeg => "-" ,
1136
1146
} ;
1147
+ let operator_len = operator_str. len ( ) ;
1137
1148
1138
- let subexpr =
1139
- try_opt ! ( expr. rewrite( context, try_opt!( width. checked_sub( operator_str. len( ) ) ) , offset) ) ;
1140
-
1141
- Some ( format ! ( "{}{}" , operator_str, subexpr) )
1149
+ expr. rewrite ( context, try_opt ! ( width. checked_sub( operator_len) ) , offset + operator_len)
1150
+ . map ( |r| format ! ( "{}{}" , operator_str, r) )
1142
1151
}
1143
1152
1144
1153
fn rewrite_assignment ( context : & RewriteContext ,
0 commit comments