@@ -841,7 +841,8 @@ pub(crate) fn format_impl(
841
841
where_span_end,
842
842
self_ty. span . hi ( ) ,
843
843
option,
844
- ) ?;
844
+ )
845
+ . ok ( ) ?;
845
846
846
847
// If there is no where-clause, we may have missing comments between the trait name and
847
848
// the opening brace.
@@ -1232,7 +1233,8 @@ pub(crate) fn format_trait(
1232
1233
None ,
1233
1234
pos_before_where,
1234
1235
option,
1235
- ) ?;
1236
+ )
1237
+ . ok ( ) ?;
1236
1238
// If the where-clause cannot fit on the same line,
1237
1239
// put the where-clause on a new line
1238
1240
if !where_clause_str. contains ( '\n' )
@@ -1337,7 +1339,11 @@ pub(crate) struct TraitAliasBounds<'a> {
1337
1339
1338
1340
impl < ' a > Rewrite for TraitAliasBounds < ' a > {
1339
1341
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1340
- let generic_bounds_str = self . generic_bounds . rewrite ( context, shape) ?;
1342
+ self . rewrite_result ( context, shape) . ok ( )
1343
+ }
1344
+
1345
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
1346
+ let generic_bounds_str = self . generic_bounds . rewrite_result ( context, shape) ?;
1341
1347
1342
1348
let mut option = WhereClauseOption :: new ( true , WhereClauseSpace :: None ) ;
1343
1349
option. allow_single_line ( ) ;
@@ -1366,7 +1372,7 @@ impl<'a> Rewrite for TraitAliasBounds<'a> {
1366
1372
shape. indent . to_string_with_newline ( context. config )
1367
1373
} ;
1368
1374
1369
- Some ( format ! ( "{generic_bounds_str}{space}{where_str}" ) )
1375
+ Ok ( format ! ( "{generic_bounds_str}{space}{where_str}" ) )
1370
1376
}
1371
1377
}
1372
1378
@@ -1624,7 +1630,8 @@ fn format_tuple_struct(
1624
1630
None ,
1625
1631
body_hi,
1626
1632
option,
1627
- ) ?
1633
+ )
1634
+ . ok ( ) ?
1628
1635
}
1629
1636
None => "" . to_owned ( ) ,
1630
1637
} ;
@@ -1793,7 +1800,8 @@ fn rewrite_ty<R: Rewrite>(
1793
1800
None ,
1794
1801
generics. span . hi ( ) ,
1795
1802
option,
1796
- ) ?;
1803
+ )
1804
+ . ok ( ) ?;
1797
1805
result. push_str ( & where_clause_str) ;
1798
1806
1799
1807
if let Some ( ty) = rhs {
@@ -2664,7 +2672,8 @@ fn rewrite_fn_base(
2664
2672
Some ( span. hi ( ) ) ,
2665
2673
pos_before_where,
2666
2674
option,
2667
- ) ?;
2675
+ )
2676
+ . ok ( ) ?;
2668
2677
// If there are neither where-clause nor return type, we may be missing comments between
2669
2678
// params and `{`.
2670
2679
if where_clause_str. is_empty ( ) {
@@ -2940,7 +2949,7 @@ fn rewrite_where_clause_rfc_style(
2940
2949
span_end : Option < BytePos > ,
2941
2950
span_end_before_where : BytePos ,
2942
2951
where_clause_option : WhereClauseOption ,
2943
- ) -> Option < String > {
2952
+ ) -> RewriteResult {
2944
2953
let ( where_keyword, allow_single_line) = rewrite_where_keyword (
2945
2954
context,
2946
2955
predicates,
@@ -2954,8 +2963,9 @@ fn rewrite_where_clause_rfc_style(
2954
2963
let clause_shape = shape
2955
2964
. block ( )
2956
2965
. with_max_width ( context. config )
2957
- . block_left ( context. config . tab_spaces ( ) ) ?
2958
- . sub_width ( 1 ) ?;
2966
+ . block_left ( context. config . tab_spaces ( ) )
2967
+ . and_then ( |s| s. sub_width ( 1 ) )
2968
+ . max_width_error ( shape. width , where_span) ?;
2959
2969
let force_single_line = context. config . where_single_line ( )
2960
2970
&& predicates. len ( ) == 1
2961
2971
&& !where_clause_option. veto_single_line ;
@@ -2980,7 +2990,7 @@ fn rewrite_where_clause_rfc_style(
2980
2990
clause_shape. indent . to_string_with_newline ( context. config )
2981
2991
} ;
2982
2992
2983
- Some ( format ! ( "{where_keyword}{clause_sep}{preds_str}" ) )
2993
+ Ok ( format ! ( "{where_keyword}{clause_sep}{preds_str}" ) )
2984
2994
}
2985
2995
2986
2996
/// Rewrite `where` and comment around it.
@@ -2991,12 +3001,13 @@ fn rewrite_where_keyword(
2991
3001
shape : Shape ,
2992
3002
span_end_before_where : BytePos ,
2993
3003
where_clause_option : WhereClauseOption ,
2994
- ) -> Option < ( String , bool ) > {
3004
+ ) -> Result < ( String , bool ) , RewriteError > {
2995
3005
let block_shape = shape. block ( ) . with_max_width ( context. config ) ;
2996
3006
// 1 = `,`
2997
3007
let clause_shape = block_shape
2998
- . block_left ( context. config . tab_spaces ( ) ) ?
2999
- . sub_width ( 1 ) ?;
3008
+ . block_left ( context. config . tab_spaces ( ) )
3009
+ . and_then ( |s| s. sub_width ( 1 ) )
3010
+ . max_width_error ( block_shape. width , where_span) ?;
3000
3011
3001
3012
let comment_separator = |comment : & str , shape : Shape | {
3002
3013
if comment. is_empty ( ) {
@@ -3027,7 +3038,7 @@ fn rewrite_where_keyword(
3027
3038
&& comment_before. is_empty ( )
3028
3039
&& comment_after. is_empty ( ) ;
3029
3040
3030
- Some ( ( result, allow_single_line) )
3041
+ Ok ( ( result, allow_single_line) )
3031
3042
}
3032
3043
3033
3044
/// Rewrite bounds on a where clause.
@@ -3039,7 +3050,7 @@ fn rewrite_bounds_on_where_clause(
3039
3050
span_end : Option < BytePos > ,
3040
3051
where_clause_option : WhereClauseOption ,
3041
3052
force_single_line : bool ,
3042
- ) -> Option < String > {
3053
+ ) -> RewriteResult {
3043
3054
let span_start = predicates[ 0 ] . span ( ) . lo ( ) ;
3044
3055
// If we don't have the start of the next span, then use the end of the
3045
3056
// predicates, but that means we miss comments.
@@ -3078,7 +3089,7 @@ fn rewrite_bounds_on_where_clause(
3078
3089
. tactic ( shape_tactic)
3079
3090
. trailing_separator ( comma_tactic)
3080
3091
. preserve_newline ( preserve_newline) ;
3081
- write_list ( & items. collect :: < Vec < _ > > ( ) , & fmt) . ok ( )
3092
+ write_list ( & items. collect :: < Vec < _ > > ( ) , & fmt)
3082
3093
}
3083
3094
3084
3095
fn rewrite_where_clause (
@@ -3092,9 +3103,9 @@ fn rewrite_where_clause(
3092
3103
span_end : Option < BytePos > ,
3093
3104
span_end_before_where : BytePos ,
3094
3105
where_clause_option : WhereClauseOption ,
3095
- ) -> Option < String > {
3106
+ ) -> RewriteResult {
3096
3107
if predicates. is_empty ( ) {
3097
- return Some ( String :: new ( ) ) ;
3108
+ return Ok ( String :: new ( ) ) ;
3098
3109
}
3099
3110
3100
3111
if context. config . indent_style ( ) == IndentStyle :: Block {
@@ -3154,7 +3165,7 @@ fn rewrite_where_clause(
3154
3165
. trailing_separator ( comma_tactic)
3155
3166
. ends_with_newline ( tactic. ends_with_newline ( context. config . indent_style ( ) ) )
3156
3167
. preserve_newline ( true ) ;
3157
- let preds_str = write_list ( & item_vec, & fmt) . ok ( ) ?;
3168
+ let preds_str = write_list ( & item_vec, & fmt) ?;
3158
3169
3159
3170
let end_length = if terminator == "{" {
3160
3171
// If the brace is on the next line we don't need to count it otherwise it needs two
@@ -3172,13 +3183,13 @@ fn rewrite_where_clause(
3172
3183
|| preds_str. contains ( '\n' )
3173
3184
|| shape. indent . width ( ) + " where " . len ( ) + preds_str. len ( ) + end_length > shape. width
3174
3185
{
3175
- Some ( format ! (
3186
+ Ok ( format ! (
3176
3187
"\n {}where {}" ,
3177
3188
( shape. indent + extra_indent) . to_string( context. config) ,
3178
3189
preds_str
3179
3190
) )
3180
3191
} else {
3181
- Some ( format ! ( " where {preds_str}" ) )
3192
+ Ok ( format ! ( " where {preds_str}" ) )
3182
3193
}
3183
3194
}
3184
3195
@@ -3199,15 +3210,14 @@ fn rewrite_comments_before_after_where(
3199
3210
span_before_where : Span ,
3200
3211
span_after_where : Span ,
3201
3212
shape : Shape ,
3202
- ) -> Option < ( String , String ) > {
3203
- let before_comment = rewrite_missing_comment ( span_before_where, shape, context) . ok ( ) ?;
3213
+ ) -> Result < ( String , String ) , RewriteError > {
3214
+ let before_comment = rewrite_missing_comment ( span_before_where, shape, context) ?;
3204
3215
let after_comment = rewrite_missing_comment (
3205
3216
span_after_where,
3206
3217
shape. block_indent ( context. config . tab_spaces ( ) ) ,
3207
3218
context,
3208
- )
3209
- . ok ( ) ?;
3210
- Some ( ( before_comment, after_comment) )
3219
+ ) ?;
3220
+ Ok ( ( before_comment, after_comment) )
3211
3221
}
3212
3222
3213
3223
fn format_header (
@@ -3289,7 +3299,8 @@ fn format_generics(
3289
3299
Some ( span. hi ( ) ) ,
3290
3300
span_end_before_where,
3291
3301
option,
3292
- ) ?;
3302
+ )
3303
+ . ok ( ) ?;
3293
3304
result. push_str ( & where_clause_str) ;
3294
3305
(
3295
3306
brace_pos == BracePos :: ForceSameLine || brace_style == BraceStyle :: PreferSameLine ,
0 commit comments