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