@@ -219,7 +219,8 @@ fn rewrite_closure(capture: ast::CaptureClause,
219219
220220 // 1 = the separating space between arguments and the body.
221221 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) ;
223224
224225 // Checks if rewrite succeeded and fits on a single line.
225226 let accept_rewrite = rewrite. as_ref ( ) . map ( |result| !result. contains ( '\n' ) ) . unwrap_or ( false ) ;
@@ -263,7 +264,8 @@ impl Rewrite for ast::Block {
263264
264265 if !trimmed. is_empty ( ) {
265266 // 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 ) )
267269 } else {
268270 "unsafe " . to_owned ( )
269271 }
@@ -357,7 +359,7 @@ impl<'a> Rewrite for Loop<'a> {
357359 fn rewrite ( & self , context : & RewriteContext , width : usize , offset : usize ) -> Option < String > {
358360 let label_string = rewrite_label ( self . label ) ;
359361 // 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( ) ) ) ;
361363 let inner_offset = offset + self . keyword . len ( ) + label_string. len ( ) ;
362364
363365 let pat_expr_string = match self . cond {
@@ -393,14 +395,17 @@ fn rewrite_range(context: &RewriteContext,
393395 offset : usize )
394396 -> Option < String > {
395397 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+ }
397403 None => String :: new ( ) ,
398404 } ;
399405
400406 let right_string = match right {
401407 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 ) ) ;
404409 try_opt ! ( expr. rewrite( context, max_width, offset + 2 + left_string. len( ) ) )
405410 }
406411 None => String :: new ( ) ,
@@ -532,7 +537,8 @@ fn rewrite_match(context: &RewriteContext,
532537 }
533538
534539 // `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 ) ) ;
536542 let mut result = format ! ( "match {} {{" , cond_str) ;
537543
538544 let block_indent = context. block_indent ;
@@ -632,17 +638,20 @@ impl Rewrite for ast::Arm {
632638 } ;
633639
634640 // 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+ } )
639648 . collect:: <Option <Vec <_>>>( ) ) ;
640649
641650 let mut total_width = pat_strs. iter ( ) . fold ( 0 , |a, p| a + p. len ( ) ) ;
642651 // Add ` | `.len().
643652 total_width += ( pat_strs. len ( ) - 1 ) * 3 ;
644653
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' ) ) ;
646655 if !vertical {
647656 // If the patterns were previously stacked, keep them stacked.
648657 // FIXME should be an option.
@@ -710,9 +719,8 @@ impl Rewrite for ast::Arm {
710719 return None ;
711720 }
712721
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) ) ;
716724 Some ( format ! ( "{}{} =>\n {}{}," ,
717725 attr_str. trim_left( ) ,
718726 pats_str,
@@ -775,9 +783,8 @@ fn rewrite_pat_expr(context: &RewriteContext,
775783 let pat_offset = offset + matcher. len ( ) ;
776784 let mut result = match pat {
777785 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) ) ;
781788 format ! ( "{}{}{}" , matcher, pat_string, connector)
782789 }
783790 None => String :: new ( ) ,
@@ -930,7 +937,8 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
930937 }
931938
932939 // 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) ) ;
934942
935943 // Foo { a: Foo } - indent is +3, width is -5.
936944 let h_budget = try_opt ! ( width. checked_sub( path_str. len( ) + 5 ) ) ;
@@ -1041,7 +1049,8 @@ fn rewrite_tuple_lit(context: &RewriteContext,
10411049 // In case of length 1, need a trailing comma
10421050 if items. len ( ) == 1 {
10431051 // 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) ) ;
10451054 }
10461055
10471056 let items = itemize_list ( context. codemap ,
@@ -1057,7 +1066,8 @@ fn rewrite_tuple_lit(context: &RewriteContext,
10571066 span. lo + BytePos ( 1 ) , // Remove parens
10581067 span. hi - BytePos ( 1 ) ) ;
10591068
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) ;
10611071
10621072 Some ( format ! ( "({})" , write_list( & items. collect:: <Vec <_>>( ) , & fmt) ) )
10631073}
@@ -1134,11 +1144,10 @@ fn rewrite_unary_op(context: &RewriteContext,
11341144 ast:: UnOp :: UnNot => "!" ,
11351145 ast:: UnOp :: UnNeg => "-" ,
11361146 } ;
1147+ let operator_len = operator_str. len ( ) ;
11371148
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) )
11421151}
11431152
11441153fn rewrite_assignment ( context : & RewriteContext ,
0 commit comments