@@ -34,11 +34,14 @@ pub(crate) fn rewrite_all_pairs(
34
34
shape : Shape ,
35
35
context : & RewriteContext < ' _ > ,
36
36
) -> Option < String > {
37
- expr. flatten ( context, shape) . and_then ( |list| {
37
+ let list = expr. flatten ( context, shape) ?;
38
+ if !list. force_multi_line {
38
39
// First we try formatting on one line.
39
- rewrite_pairs_one_line ( & list, shape, context)
40
- . or_else ( || rewrite_pairs_multiline ( & list, shape, context) )
41
- } )
40
+ if let Some ( out) = rewrite_pairs_one_line ( & list, shape, context) {
41
+ return Some ( out) ;
42
+ }
43
+ }
44
+ rewrite_pairs_multiline ( & list, shape, context)
42
45
}
43
46
44
47
// This may return a multi-line result since we allow the last expression to go
@@ -246,6 +249,7 @@ trait FlattenPair: Rewrite + Sized {
246
249
struct PairList < ' a , ' b , T : Rewrite > {
247
250
list : Vec < ( & ' b T , Option < String > ) > ,
248
251
separators : Vec < & ' a str > ,
252
+ force_multi_line : bool ,
249
253
}
250
254
251
255
impl FlattenPair for ast:: Expr {
@@ -283,6 +287,7 @@ impl FlattenPair for ast::Expr {
283
287
let mut stack = vec ! [ ] ;
284
288
let mut list = vec ! [ ] ;
285
289
let mut separators = vec ! [ ] ;
290
+ let mut force_multi_line = false ;
286
291
let mut node = self ;
287
292
loop {
288
293
match node. kind {
@@ -293,6 +298,9 @@ impl FlattenPair for ast::Expr {
293
298
_ => {
294
299
let op_len = separators. last ( ) . map_or ( 0 , |s : & & str | s. len ( ) ) ;
295
300
let rw = default_rewrite ( node, op_len, list. is_empty ( ) ) ;
301
+ // a `let` expression forces multi-line, unless it is the first expression
302
+ force_multi_line |=
303
+ !list. is_empty ( ) && matches ! ( node. kind, ast:: ExprKind :: Let ( ..) ) ;
296
304
list. push ( ( node, rw) ) ;
297
305
if let Some ( pop) = stack. pop ( ) {
298
306
match pop. kind {
@@ -310,7 +318,11 @@ impl FlattenPair for ast::Expr {
310
318
}
311
319
312
320
assert_eq ! ( list. len( ) - 1 , separators. len( ) ) ;
313
- Some ( PairList { list, separators } )
321
+ Some ( PairList {
322
+ list,
323
+ separators,
324
+ force_multi_line,
325
+ } )
314
326
}
315
327
}
316
328
0 commit comments