@@ -34,11 +34,14 @@ pub(crate) fn rewrite_all_pairs(
3434 shape : Shape ,
3535 context : & RewriteContext < ' _ > ,
3636) -> Option < String > {
37- expr. flatten ( context, shape) . and_then ( |list| {
37+ let list = expr. flatten ( context, shape) ?;
38+ if !list. force_multi_line {
3839 // 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)
4245}
4346
4447// This may return a multi-line result since we allow the last expression to go
@@ -246,6 +249,7 @@ trait FlattenPair: Rewrite + Sized {
246249struct PairList < ' a , ' b , T : Rewrite > {
247250 list : Vec < ( & ' b T , Option < String > ) > ,
248251 separators : Vec < & ' a str > ,
252+ force_multi_line : bool ,
249253}
250254
251255impl FlattenPair for ast:: Expr {
@@ -283,6 +287,7 @@ impl FlattenPair for ast::Expr {
283287 let mut stack = vec ! [ ] ;
284288 let mut list = vec ! [ ] ;
285289 let mut separators = vec ! [ ] ;
290+ let mut force_multi_line = false ;
286291 let mut node = self ;
287292 loop {
288293 match node. kind {
@@ -293,6 +298,9 @@ impl FlattenPair for ast::Expr {
293298 _ => {
294299 let op_len = separators. last ( ) . map_or ( 0 , |s : & & str | s. len ( ) ) ;
295300 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 ( ..) ) ;
296304 list. push ( ( node, rw) ) ;
297305 if let Some ( pop) = stack. pop ( ) {
298306 match pop. kind {
@@ -310,7 +318,11 @@ impl FlattenPair for ast::Expr {
310318 }
311319
312320 assert_eq ! ( list. len( ) - 1 , separators. len( ) ) ;
313- Some ( PairList { list, separators } )
321+ Some ( PairList {
322+ list,
323+ separators,
324+ force_multi_line,
325+ } )
314326 }
315327}
316328
0 commit comments