@@ -593,6 +593,7 @@ pub fn format_impl(
593
593
where_span_end,
594
594
self_ty. span . hi ( ) ,
595
595
option,
596
+ false ,
596
597
) ?;
597
598
598
599
// If there is no where clause, we may have missing comments between the trait name and
@@ -960,6 +961,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
960
961
None ,
961
962
pos_before_where,
962
963
option,
964
+ false ,
963
965
) ?;
964
966
// If the where clause cannot fit on the same line,
965
967
// put the where clause on a new line
@@ -1229,6 +1231,7 @@ fn format_tuple_struct(
1229
1231
None ,
1230
1232
body_hi,
1231
1233
option,
1234
+ false ,
1232
1235
) ?
1233
1236
}
1234
1237
None => "" . to_owned ( ) ,
@@ -1321,6 +1324,7 @@ pub fn rewrite_type_alias(
1321
1324
Some ( span. hi ( ) ) ,
1322
1325
generics. span . hi ( ) ,
1323
1326
option,
1327
+ false ,
1324
1328
) ?;
1325
1329
result. push_str ( & where_clause_str) ;
1326
1330
if where_clause_str. is_empty ( ) {
@@ -1954,6 +1958,8 @@ fn rewrite_fn_base(
1954
1958
ast:: FunctionRetTy :: Ty ( ref ty) => ty. span . hi ( ) ,
1955
1959
} ;
1956
1960
1961
+ let is_args_multi_lined = arg_str. contains ( '\n' ) ;
1962
+
1957
1963
if where_clause. predicates . len ( ) == 1 && should_compress_where {
1958
1964
let budget = context. budget ( last_line_used_width ( & result, indent. width ( ) ) ) ;
1959
1965
if let Some ( where_clause_str) = rewrite_where_clause (
@@ -1966,6 +1972,7 @@ fn rewrite_fn_base(
1966
1972
Some ( span. hi ( ) ) ,
1967
1973
pos_before_where,
1968
1974
WhereClauseOption :: compressed ( ) ,
1975
+ is_args_multi_lined,
1969
1976
) {
1970
1977
result. push_str ( & where_clause_str) ;
1971
1978
force_new_line_for_brace |= last_line_contains_single_line_comment ( & result) ;
@@ -1984,6 +1991,7 @@ fn rewrite_fn_base(
1984
1991
Some ( span. hi ( ) ) ,
1985
1992
pos_before_where,
1986
1993
option,
1994
+ is_args_multi_lined,
1987
1995
) ?;
1988
1996
// If there are neither where clause nor return type, we may be missing comments between
1989
1997
// args and `{`.
@@ -2007,6 +2015,7 @@ fn rewrite_fn_base(
2007
2015
result. push_str ( & where_clause_str) ;
2008
2016
2009
2017
force_new_line_for_brace |= last_line_contains_single_line_comment ( & result) ;
2018
+ force_new_line_for_brace |= is_args_multi_lined && context. config . where_single_line ( ) ;
2010
2019
Some ( ( result, force_new_line_for_brace) )
2011
2020
}
2012
2021
@@ -2264,11 +2273,16 @@ fn compute_budgets_for_args(
2264
2273
}
2265
2274
2266
2275
fn newline_for_brace ( config : & Config , where_clause : & ast:: WhereClause , has_body : bool ) -> bool {
2276
+ let predicate_count = where_clause. predicates . len ( ) ;
2277
+
2278
+ if config. where_single_line ( ) && predicate_count == 1 {
2279
+ return false ;
2280
+ }
2267
2281
match ( config. fn_brace_style ( ) , config. where_density ( ) ) {
2268
2282
( BraceStyle :: AlwaysNextLine , _) => true ,
2269
- ( _, Density :: Compressed ) if where_clause . predicates . len ( ) == 1 => false ,
2270
- ( _, Density :: CompressedIfEmpty ) if where_clause . predicates . len ( ) == 1 && !has_body => false ,
2271
- ( BraceStyle :: SameLineWhere , _) if !where_clause . predicates . is_empty ( ) => true ,
2283
+ ( _, Density :: Compressed ) if predicate_count == 1 => false ,
2284
+ ( _, Density :: CompressedIfEmpty ) if predicate_count == 1 && !has_body => false ,
2285
+ ( BraceStyle :: SameLineWhere , _) if predicate_count > 0 => true ,
2272
2286
_ => false ,
2273
2287
}
2274
2288
}
@@ -2444,6 +2458,7 @@ fn rewrite_where_clause_rfc_style(
2444
2458
span_end : Option < BytePos > ,
2445
2459
span_end_before_where : BytePos ,
2446
2460
where_clause_option : WhereClauseOption ,
2461
+ is_args_multi_line : bool ,
2447
2462
) -> Option < String > {
2448
2463
let block_shape = shape. block ( ) . with_max_width ( context. config ) ;
2449
2464
@@ -2479,14 +2494,23 @@ fn rewrite_where_clause_rfc_style(
2479
2494
span_end,
2480
2495
false ,
2481
2496
) ;
2482
- let comma_tactic = if where_clause_option. suppress_comma {
2497
+ let where_single_line = context. config . where_single_line ( ) && len == 1 && !is_args_multi_line;
2498
+ let comma_tactic = if where_clause_option. suppress_comma || where_single_line {
2483
2499
SeparatorTactic :: Never
2484
2500
} else {
2485
2501
context. config . trailing_comma ( )
2486
2502
} ;
2487
2503
2504
+ // shape should be vertical only and only if we have `where_single_line` option enabled
2505
+ // and the number of items of the where clause is equal to 1
2506
+ let shape_tactic = if where_single_line {
2507
+ DefinitiveListTactic :: Horizontal
2508
+ } else {
2509
+ DefinitiveListTactic :: Vertical
2510
+ } ;
2511
+
2488
2512
let fmt = ListFormatting {
2489
- tactic : DefinitiveListTactic :: Vertical ,
2513
+ tactic : shape_tactic ,
2490
2514
separator : "," ,
2491
2515
trailing_separator : comma_tactic,
2492
2516
separator_place : SeparatorPlace :: Back ,
@@ -2508,7 +2532,7 @@ fn rewrite_where_clause_rfc_style(
2508
2532
// 6 = `where `
2509
2533
let clause_sep = if where_clause_option. compress_where && comment_before. is_empty ( )
2510
2534
&& comment_after. is_empty ( ) && !preds_str. contains ( '\n' )
2511
- && 6 + preds_str. len ( ) <= shape. width
2535
+ && 6 + preds_str. len ( ) <= shape. width || where_single_line
2512
2536
{
2513
2537
String :: from ( " " )
2514
2538
} else {
@@ -2536,6 +2560,7 @@ fn rewrite_where_clause(
2536
2560
span_end : Option < BytePos > ,
2537
2561
span_end_before_where : BytePos ,
2538
2562
where_clause_option : WhereClauseOption ,
2563
+ is_args_multi_line : bool ,
2539
2564
) -> Option < String > {
2540
2565
if where_clause. predicates . is_empty ( ) {
2541
2566
return Some ( String :: new ( ) ) ;
@@ -2550,6 +2575,7 @@ fn rewrite_where_clause(
2550
2575
span_end,
2551
2576
span_end_before_where,
2552
2577
where_clause_option,
2578
+ is_args_multi_line,
2553
2579
) ;
2554
2580
}
2555
2581
@@ -2698,6 +2724,7 @@ fn format_generics(
2698
2724
Some ( span. hi ( ) ) ,
2699
2725
span_end_before_where,
2700
2726
option,
2727
+ false ,
2701
2728
) ?;
2702
2729
result. push_str ( & where_clause_str) ;
2703
2730
force_same_line_brace || brace_style == BraceStyle :: PreferSameLine
0 commit comments