@@ -387,8 +387,6 @@ impl<'a> FmtVisitor<'a> {
387
387
Some ( ref body_str) => self . buffer . push_str ( body_str) ,
388
388
None => if contains_comment ( & enum_snippet[ brace_pos..] ) {
389
389
self . format_missing_no_indent ( span. hi ( ) - BytePos ( 1 ) )
390
- } else {
391
- self . format_missing ( span. hi ( ) - BytePos ( 1 ) )
392
390
} ,
393
391
}
394
392
self . block_indent = self . block_indent . block_unindent ( self . config ) ;
@@ -535,11 +533,7 @@ pub fn format_impl(
535
533
let where_budget = if result. contains ( '\n' ) {
536
534
context. config . max_width ( )
537
535
} else {
538
- context
539
- . config
540
- . max_width ( )
541
- . checked_sub ( last_line_width ( & result) )
542
- . unwrap_or ( 0 )
536
+ context. budget ( last_line_width ( & result) )
543
537
} ;
544
538
let option = WhereClauseOption :: snuggled ( & ref_and_type) ;
545
539
let where_clause_str = try_opt ! ( rewrite_where_clause(
@@ -758,11 +752,7 @@ fn format_impl_ref_and_type(
758
752
} ;
759
753
let used_space = last_line_width ( & result) + trait_ref_overhead + curly_brace_overhead;
760
754
// 1 = space before the type.
761
- let budget = context
762
- . config
763
- . max_width ( )
764
- . checked_sub ( used_space + 1 )
765
- . unwrap_or ( 0 ) ;
755
+ let budget = context. budget ( used_space + 1 ) ;
766
756
if let Some ( self_ty_str) = self_ty. rewrite ( context, Shape :: legacy ( budget, offset) ) {
767
757
if !self_ty_str. contains ( '\n' ) {
768
758
if trait_ref. is_some ( ) {
@@ -783,7 +773,7 @@ fn format_impl_ref_and_type(
783
773
if trait_ref. is_some ( ) {
784
774
result. push_str ( "for " ) ;
785
775
}
786
- let budget = context. config . max_width ( ) - last_line_width ( & result) ;
776
+ let budget = context. budget ( last_line_width ( & result) ) ;
787
777
let type_offset = match context. config . where_style ( ) {
788
778
Style :: Legacy => new_line_offset + trait_ref_overhead,
789
779
Style :: Rfc => new_line_offset,
@@ -927,12 +917,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
927
917
Density :: Tall
928
918
} ;
929
919
930
- let where_budget = try_opt ! (
931
- context
932
- . config
933
- . max_width( )
934
- . checked_sub( last_line_width( & result) )
935
- ) ;
920
+ let where_budget = context. budget ( last_line_width ( & result) ) ;
936
921
let pos_before_where = if type_param_bounds. is_empty ( ) {
937
922
generics. where_clause . span . lo ( )
938
923
} else {
@@ -1078,11 +1063,7 @@ pub fn format_struct_struct(
1078
1063
let overhead = if fields. is_empty ( ) { 3 } else { 2 } ;
1079
1064
if ( context. config . item_brace_style ( ) == BraceStyle :: AlwaysNextLine &&
1080
1065
!fields. is_empty ( ) ) ||
1081
- context
1082
- . config
1083
- . max_width ( )
1084
- . checked_sub ( result. len ( ) )
1085
- . unwrap_or ( 0 ) < overhead
1066
+ context. config . max_width ( ) < overhead + result. len ( )
1086
1067
{
1087
1068
format ! ( "\n {}{{" , offset. block_only( ) . to_string( context. config) )
1088
1069
} else {
@@ -1092,12 +1073,10 @@ pub fn format_struct_struct(
1092
1073
} ;
1093
1074
// 1 = `}`
1094
1075
let overhead = if fields. is_empty ( ) { 1 } else { 0 } ;
1095
- let max_len = context
1096
- . config
1097
- . max_width ( )
1098
- . checked_sub ( offset. width ( ) )
1099
- . unwrap_or ( 0 ) ;
1100
- if !generics_str. contains ( '\n' ) && result. len ( ) + generics_str. len ( ) + overhead > max_len {
1076
+ let total_width = result. len ( ) + generics_str. len ( ) + overhead;
1077
+ if !generics_str. is_empty ( ) && !generics_str. contains ( '\n' ) &&
1078
+ total_width > context. config . max_width ( )
1079
+ {
1101
1080
result. push ( '\n' ) ;
1102
1081
result. push_str ( & offset. to_string ( context. config ) ) ;
1103
1082
result. push_str ( & generics_str. trim_left ( ) ) ;
@@ -1122,11 +1101,7 @@ pub fn format_struct_struct(
1122
1101
}
1123
1102
1124
1103
// 3 = ` ` and ` }`
1125
- let one_line_budget = context
1126
- . config
1127
- . max_width ( )
1128
- . checked_sub ( result. len ( ) + 3 + offset. width ( ) )
1129
- . unwrap_or ( 0 ) ;
1104
+ let one_line_budget = context. budget ( result. len ( ) + 3 + offset. width ( ) ) ;
1130
1105
let one_line_budget =
1131
1106
one_line_width. map_or ( 0 , |one_line_width| min ( one_line_width, one_line_budget) ) ;
1132
1107
@@ -1286,12 +1261,7 @@ pub fn rewrite_type_alias(
1286
1261
let generics_str = try_opt ! ( rewrite_generics( context, generics, shape, g_span) ) ;
1287
1262
result. push_str ( & generics_str) ;
1288
1263
1289
- let where_budget = try_opt ! (
1290
- context
1291
- . config
1292
- . max_width( )
1293
- . checked_sub( last_line_width( & result) )
1294
- ) ;
1264
+ let where_budget = context. budget ( last_line_width ( & result) ) ;
1295
1265
let option = WhereClauseOption :: snuggled ( & result) ;
1296
1266
let where_clause_str = try_opt ! ( rewrite_where_clause(
1297
1267
context,
@@ -1314,11 +1284,7 @@ pub fn rewrite_type_alias(
1314
1284
let line_width = last_line_width ( & result) ;
1315
1285
// This checked_sub may fail as the extra space after '=' is not taken into account
1316
1286
// In that case the budget is set to 0 which will make ty.rewrite retry on a new line
1317
- let budget = context
1318
- . config
1319
- . max_width ( )
1320
- . checked_sub ( indent. width ( ) + line_width + ";" . len ( ) )
1321
- . unwrap_or ( 0 ) ;
1287
+ let budget = context. budget ( indent. width ( ) + line_width + ";" . len ( ) ) ;
1322
1288
let type_indent = indent + line_width;
1323
1289
// Try to fit the type on the same line
1324
1290
let ty_str = try_opt ! (
@@ -1331,12 +1297,7 @@ pub fn rewrite_type_alias(
1331
1297
let type_indent = indent. block_indent( context. config) ;
1332
1298
result. push( '\n' ) ;
1333
1299
result. push_str( & type_indent. to_string( context. config) ) ;
1334
- let budget = try_opt!(
1335
- context
1336
- . config
1337
- . max_width( )
1338
- . checked_sub( type_indent. width( ) + ";" . len( ) )
1339
- ) ;
1300
+ let budget = context. budget( type_indent. width( ) + ";" . len( ) ) ;
1340
1301
ty. rewrite( context, Shape :: legacy( budget, type_indent) )
1341
1302
} )
1342
1303
) ;
@@ -1520,7 +1481,7 @@ pub fn rewrite_static(
1520
1481
if let Some ( expr) = expr_opt {
1521
1482
let lhs = format ! ( "{}{} =" , prefix, ty_str) ;
1522
1483
// 1 = ;
1523
- let remaining_width = context. config . max_width ( ) - offset. block_indent - 1 ;
1484
+ let remaining_width = context. budget ( offset. block_indent + 1 ) ;
1524
1485
rewrite_assign_rhs (
1525
1486
context,
1526
1487
lhs,
@@ -1567,7 +1528,7 @@ pub fn rewrite_associated_type(
1567
1528
let ty_str = try_opt ! ( ty. rewrite(
1568
1529
context,
1569
1530
Shape :: legacy(
1570
- context. config . max_width ( ) - indent. block_indent - prefix. len( ) - 2 ,
1531
+ context. budget ( indent. block_indent + prefix. len( ) + 2 ) ,
1571
1532
indent. block_only( ) ,
1572
1533
) ,
1573
1534
) ) ;
@@ -1775,11 +1736,7 @@ fn rewrite_fn_base(
1775
1736
2
1776
1737
} ;
1777
1738
let used_width = last_line_used_width ( & result, indent. width ( ) ) ;
1778
- let one_line_budget = context
1779
- . config
1780
- . max_width ( )
1781
- . checked_sub ( used_width + overhead)
1782
- . unwrap_or ( 0 ) ;
1739
+ let one_line_budget = context. budget ( used_width + overhead) ;
1783
1740
let shape = Shape {
1784
1741
width : one_line_budget,
1785
1742
indent : indent,
@@ -2015,11 +1972,7 @@ fn rewrite_fn_base(
2015
1972
} ;
2016
1973
2017
1974
if where_clause. predicates . len ( ) == 1 && should_compress_where {
2018
- let budget = context
2019
- . config
2020
- . max_width ( )
2021
- . checked_sub ( last_line_used_width ( & result, indent. width ( ) ) )
2022
- . unwrap_or ( 0 ) ;
1975
+ let budget = context. budget ( last_line_used_width ( & result, indent. width ( ) ) ) ;
2023
1976
if let Some ( where_clause_str) = rewrite_where_clause (
2024
1977
context,
2025
1978
where_clause,
@@ -2298,27 +2251,19 @@ fn compute_budgets_for_args(
2298
2251
// 1 = `;`
2299
2252
used_space += 1 ;
2300
2253
}
2301
- let one_line_budget = context
2302
- . config
2303
- . max_width ( )
2304
- . checked_sub ( used_space)
2305
- . unwrap_or ( 0 ) ;
2254
+ let one_line_budget = context. budget ( used_space) ;
2306
2255
2307
2256
if one_line_budget > 0 {
2308
2257
// 4 = "() {".len()
2309
2258
let ( indent, multi_line_budget) = match context. config . fn_args_layout ( ) {
2310
2259
IndentStyle :: Block => {
2311
2260
let indent = indent. block_indent ( context. config ) ;
2312
- let budget =
2313
- try_opt ! ( context. config. max_width( ) . checked_sub( indent. width( ) + 1 ) ) ;
2314
- ( indent, budget)
2261
+ ( indent, context. budget ( indent. width ( ) + 1 ) )
2315
2262
}
2316
2263
IndentStyle :: Visual => {
2317
2264
let indent = indent + result. len ( ) + 1 ;
2318
2265
let multi_line_overhead = indent. width ( ) + if newline_brace { 2 } else { 4 } ;
2319
- let budget =
2320
- try_opt ! ( context. config. max_width( ) . checked_sub( multi_line_overhead) ) ;
2321
- ( indent, budget)
2266
+ ( indent, context. budget ( multi_line_overhead) )
2322
2267
}
2323
2268
} ;
2324
2269
@@ -2334,8 +2279,7 @@ fn compute_budgets_for_args(
2334
2279
// Account for `)` and possibly ` {`.
2335
2280
IndentStyle :: Visual => new_indent. width ( ) + if ret_str_len == 0 { 1 } else { 3 } ,
2336
2281
} ;
2337
- let max_space = try_opt ! ( context. config. max_width( ) . checked_sub( used_space) ) ;
2338
- Some ( ( 0 , max_space, new_indent) )
2282
+ Some ( ( 0 , context. budget ( used_space) , new_indent) )
2339
2283
}
2340
2284
2341
2285
fn newline_for_brace ( config : & Config , where_clause : & ast:: WhereClause , has_body : bool ) -> bool {
@@ -2748,11 +2692,7 @@ fn format_generics(
2748
2692
let mut result = try_opt ! ( rewrite_generics( context, generics, shape, span) ) ;
2749
2693
2750
2694
let same_line_brace = if !generics. where_clause . predicates . is_empty ( ) || result. contains ( '\n' ) {
2751
- let budget = context
2752
- . config
2753
- . max_width ( )
2754
- . checked_sub ( last_line_used_width ( & result, offset. width ( ) ) )
2755
- . unwrap_or ( 0 ) ;
2695
+ let budget = context. budget ( last_line_used_width ( & result, offset. width ( ) ) ) ;
2756
2696
let option = WhereClauseOption :: snuggled ( & result) ;
2757
2697
let where_clause_str = try_opt ! ( rewrite_where_clause(
2758
2698
context,
@@ -2773,11 +2713,7 @@ fn format_generics(
2773
2713
brace_style != BraceStyle :: AlwaysNextLine
2774
2714
} ;
2775
2715
let total_used_width = last_line_used_width ( & result, used_width) ;
2776
- let remaining_budget = context
2777
- . config
2778
- . max_width ( )
2779
- . checked_sub ( total_used_width)
2780
- . unwrap_or ( 0 ) ;
2716
+ let remaining_budget = context. budget ( total_used_width) ;
2781
2717
// If the same line brace if forced, it indicates that we are rewriting an item with empty body,
2782
2718
// and hence we take the closer into account as well for one line budget.
2783
2719
// We assume that the closer has the same length as the opener.
0 commit comments