@@ -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.
@@ -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
@@ -1622,7 +1628,8 @@ fn format_tuple_struct(
1622
1628
None ,
1623
1629
body_hi,
1624
1630
option,
1625
- ) ?
1631
+ )
1632
+ . ok ( ) ?
1626
1633
}
1627
1634
None => "" . to_owned ( ) ,
1628
1635
} ;
@@ -1791,7 +1798,8 @@ fn rewrite_ty<R: Rewrite>(
1791
1798
None ,
1792
1799
generics. span . hi ( ) ,
1793
1800
option,
1794
- ) ?;
1801
+ )
1802
+ . ok ( ) ?;
1795
1803
result. push_str ( & where_clause_str) ;
1796
1804
1797
1805
if let Some ( ty) = rhs {
@@ -2661,7 +2669,8 @@ fn rewrite_fn_base(
2661
2669
Some ( span. hi ( ) ) ,
2662
2670
pos_before_where,
2663
2671
option,
2664
- ) ?;
2672
+ )
2673
+ . ok ( ) ?;
2665
2674
// If there are neither where-clause nor return type, we may be missing comments between
2666
2675
// params and `{`.
2667
2676
if where_clause_str. is_empty ( ) {
@@ -2937,7 +2946,7 @@ fn rewrite_where_clause_rfc_style(
2937
2946
span_end : Option < BytePos > ,
2938
2947
span_end_before_where : BytePos ,
2939
2948
where_clause_option : WhereClauseOption ,
2940
- ) -> Option < String > {
2949
+ ) -> RewriteResult {
2941
2950
let ( where_keyword, allow_single_line) = rewrite_where_keyword (
2942
2951
context,
2943
2952
predicates,
@@ -2951,8 +2960,9 @@ fn rewrite_where_clause_rfc_style(
2951
2960
let clause_shape = shape
2952
2961
. block ( )
2953
2962
. with_max_width ( context. config )
2954
- . block_left ( context. config . tab_spaces ( ) ) ?
2955
- . sub_width ( 1 ) ?;
2963
+ . block_left ( context. config . tab_spaces ( ) )
2964
+ . and_then ( |s| s. sub_width ( 1 ) )
2965
+ . max_width_error ( shape. width , where_span) ?;
2956
2966
let force_single_line = context. config . where_single_line ( )
2957
2967
&& predicates. len ( ) == 1
2958
2968
&& !where_clause_option. veto_single_line ;
@@ -2977,7 +2987,7 @@ fn rewrite_where_clause_rfc_style(
2977
2987
clause_shape. indent . to_string_with_newline ( context. config )
2978
2988
} ;
2979
2989
2980
- Some ( format ! ( "{where_keyword}{clause_sep}{preds_str}" ) )
2990
+ Ok ( format ! ( "{where_keyword}{clause_sep}{preds_str}" ) )
2981
2991
}
2982
2992
2983
2993
/// Rewrite `where` and comment around it.
@@ -2988,12 +2998,13 @@ fn rewrite_where_keyword(
2988
2998
shape : Shape ,
2989
2999
span_end_before_where : BytePos ,
2990
3000
where_clause_option : WhereClauseOption ,
2991
- ) -> Option < ( String , bool ) > {
3001
+ ) -> Result < ( String , bool ) , RewriteError > {
2992
3002
let block_shape = shape. block ( ) . with_max_width ( context. config ) ;
2993
3003
// 1 = `,`
2994
3004
let clause_shape = block_shape
2995
- . block_left ( context. config . tab_spaces ( ) ) ?
2996
- . sub_width ( 1 ) ?;
3005
+ . block_left ( context. config . tab_spaces ( ) )
3006
+ . and_then ( |s| s. sub_width ( 1 ) )
3007
+ . max_width_error ( block_shape. width , where_span) ?;
2997
3008
2998
3009
let comment_separator = |comment : & str , shape : Shape | {
2999
3010
if comment. is_empty ( ) {
@@ -3024,7 +3035,7 @@ fn rewrite_where_keyword(
3024
3035
&& comment_before. is_empty ( )
3025
3036
&& comment_after. is_empty ( ) ;
3026
3037
3027
- Some ( ( result, allow_single_line) )
3038
+ Ok ( ( result, allow_single_line) )
3028
3039
}
3029
3040
3030
3041
/// Rewrite bounds on a where clause.
@@ -3036,7 +3047,7 @@ fn rewrite_bounds_on_where_clause(
3036
3047
span_end : Option < BytePos > ,
3037
3048
where_clause_option : WhereClauseOption ,
3038
3049
force_single_line : bool ,
3039
- ) -> Option < String > {
3050
+ ) -> RewriteResult {
3040
3051
let span_start = predicates[ 0 ] . span ( ) . lo ( ) ;
3041
3052
// If we don't have the start of the next span, then use the end of the
3042
3053
// predicates, but that means we miss comments.
@@ -3075,7 +3086,7 @@ fn rewrite_bounds_on_where_clause(
3075
3086
. tactic ( shape_tactic)
3076
3087
. trailing_separator ( comma_tactic)
3077
3088
. preserve_newline ( preserve_newline) ;
3078
- write_list ( & items. collect :: < Vec < _ > > ( ) , & fmt) . ok ( )
3089
+ write_list ( & items. collect :: < Vec < _ > > ( ) , & fmt)
3079
3090
}
3080
3091
3081
3092
fn rewrite_where_clause (
@@ -3089,9 +3100,9 @@ fn rewrite_where_clause(
3089
3100
span_end : Option < BytePos > ,
3090
3101
span_end_before_where : BytePos ,
3091
3102
where_clause_option : WhereClauseOption ,
3092
- ) -> Option < String > {
3103
+ ) -> RewriteResult {
3093
3104
if predicates. is_empty ( ) {
3094
- return Some ( String :: new ( ) ) ;
3105
+ return Ok ( String :: new ( ) ) ;
3095
3106
}
3096
3107
3097
3108
if context. config . indent_style ( ) == IndentStyle :: Block {
@@ -3151,7 +3162,7 @@ fn rewrite_where_clause(
3151
3162
. trailing_separator ( comma_tactic)
3152
3163
. ends_with_newline ( tactic. ends_with_newline ( context. config . indent_style ( ) ) )
3153
3164
. preserve_newline ( true ) ;
3154
- let preds_str = write_list ( & item_vec, & fmt) . ok ( ) ?;
3165
+ let preds_str = write_list ( & item_vec, & fmt) ?;
3155
3166
3156
3167
let end_length = if terminator == "{" {
3157
3168
// If the brace is on the next line we don't need to count it otherwise it needs two
@@ -3169,13 +3180,13 @@ fn rewrite_where_clause(
3169
3180
|| preds_str. contains ( '\n' )
3170
3181
|| shape. indent . width ( ) + " where " . len ( ) + preds_str. len ( ) + end_length > shape. width
3171
3182
{
3172
- Some ( format ! (
3183
+ Ok ( format ! (
3173
3184
"\n {}where {}" ,
3174
3185
( shape. indent + extra_indent) . to_string( context. config) ,
3175
3186
preds_str
3176
3187
) )
3177
3188
} else {
3178
- Some ( format ! ( " where {preds_str}" ) )
3189
+ Ok ( format ! ( " where {preds_str}" ) )
3179
3190
}
3180
3191
}
3181
3192
@@ -3196,15 +3207,14 @@ fn rewrite_comments_before_after_where(
3196
3207
span_before_where : Span ,
3197
3208
span_after_where : Span ,
3198
3209
shape : Shape ,
3199
- ) -> Option < ( String , String ) > {
3200
- let before_comment = rewrite_missing_comment ( span_before_where, shape, context) . ok ( ) ?;
3210
+ ) -> Result < ( String , String ) , RewriteError > {
3211
+ let before_comment = rewrite_missing_comment ( span_before_where, shape, context) ?;
3201
3212
let after_comment = rewrite_missing_comment (
3202
3213
span_after_where,
3203
3214
shape. block_indent ( context. config . tab_spaces ( ) ) ,
3204
3215
context,
3205
- )
3206
- . ok ( ) ?;
3207
- Some ( ( before_comment, after_comment) )
3216
+ ) ?;
3217
+ Ok ( ( before_comment, after_comment) )
3208
3218
}
3209
3219
3210
3220
fn format_header (
@@ -3286,7 +3296,8 @@ fn format_generics(
3286
3296
Some ( span. hi ( ) ) ,
3287
3297
span_end_before_where,
3288
3298
option,
3289
- ) ?;
3299
+ )
3300
+ . ok ( ) ?;
3290
3301
result. push_str ( & where_clause_str) ;
3291
3302
(
3292
3303
brace_pos == BracePos :: ForceSameLine || brace_style == BraceStyle :: PreferSameLine ,
0 commit comments