@@ -17,7 +17,7 @@ use syntax::{ast, ptr};
17
17
18
18
use closures;
19
19
use expr:: {
20
- can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call,
20
+ can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call, is_simple_expr ,
21
21
maybe_get_args_offset,
22
22
} ;
23
23
use lists:: { definitive_tactic, itemize_list, write_list, ListFormatting , ListItem , Separator } ;
@@ -70,6 +70,30 @@ impl<'a> OverflowableItem<'a> {
70
70
}
71
71
}
72
72
73
+ pub fn is_simple ( & self ) -> bool {
74
+ match self {
75
+ OverflowableItem :: Expr ( expr) => is_simple_expr ( expr) ,
76
+ OverflowableItem :: MacroArg ( MacroArg :: Expr ( expr) ) => is_simple_expr ( expr) ,
77
+ _ => false ,
78
+ }
79
+ }
80
+
81
+ pub fn is_expr ( & self ) -> bool {
82
+ match self {
83
+ OverflowableItem :: Expr ( ..) => true ,
84
+ OverflowableItem :: MacroArg ( MacroArg :: Expr ( ..) ) => true ,
85
+ _ => false ,
86
+ }
87
+ }
88
+
89
+ pub fn is_nested_call ( & self ) -> bool {
90
+ match self {
91
+ OverflowableItem :: Expr ( expr) => is_nested_call ( expr) ,
92
+ OverflowableItem :: MacroArg ( MacroArg :: Expr ( expr) ) => is_nested_call ( expr) ,
93
+ _ => false ,
94
+ }
95
+ }
96
+
73
97
pub fn to_expr ( & self ) -> Option < & ' a ast:: Expr > {
74
98
match self {
75
99
OverflowableItem :: Expr ( expr) => Some ( expr) ,
@@ -303,23 +327,24 @@ impl<'a> Context<'a> {
303
327
shape : Shape ,
304
328
) -> Option < String > {
305
329
let last_item = self . last_item ( ) ?;
306
- let rewrite = if let Some ( expr) = last_item. to_expr ( ) {
307
- match expr. node {
308
- // When overflowing the closure which consists of a single control flow expression,
309
- // force to use block if its condition uses multi line.
310
- ast:: ExprKind :: Closure ( ..) => {
311
- // If the argument consists of multiple closures, we do not overflow
312
- // the last closure.
313
- if closures:: args_have_many_closure ( & self . items ) {
314
- None
315
- } else {
316
- closures:: rewrite_last_closure ( self . context , expr, shape)
330
+ let rewrite = match last_item {
331
+ OverflowableItem :: Expr ( ref expr) => {
332
+ match expr. node {
333
+ // When overflowing the closure which consists of a single control flow
334
+ // expression, force to use block if its condition uses multi line.
335
+ ast:: ExprKind :: Closure ( ..) => {
336
+ // If the argument consists of multiple closures, we do not overflow
337
+ // the last closure.
338
+ if closures:: args_have_many_closure ( & self . items ) {
339
+ None
340
+ } else {
341
+ closures:: rewrite_last_closure ( self . context , expr, shape)
342
+ }
317
343
}
344
+ _ => expr. rewrite ( self . context , shape) ,
318
345
}
319
- _ => expr. rewrite ( self . context , shape) ,
320
346
}
321
- } else {
322
- last_item. rewrite ( self . context , shape)
347
+ item @ _ => item. rewrite ( self . context , shape) ,
323
348
} ;
324
349
325
350
if let Some ( rewrite) = rewrite {
@@ -343,20 +368,21 @@ impl<'a> Context<'a> {
343
368
fn try_overflow_last_item ( & self , list_items : & mut Vec < ListItem > ) -> DefinitiveListTactic {
344
369
// 1 = "("
345
370
let combine_arg_with_callee = self . items . len ( ) == 1
346
- && self . items [ 0 ] . to_expr ( ) . is_some ( )
371
+ && self . items [ 0 ] . is_expr ( )
347
372
&& self . ident . len ( ) < self . context . config . tab_spaces ( ) ;
348
373
let overflow_last = combine_arg_with_callee || can_be_overflowed ( self . context , & self . items ) ;
349
374
350
375
// Replace the last item with its first line to see if it fits with
351
376
// first arguments.
352
377
let placeholder = if overflow_last {
353
378
let old_value = * self . context . force_one_line_chain . borrow ( ) ;
354
- if !combine_arg_with_callee {
355
- if let Some ( ref expr) = self . last_item ( ) . and_then ( |item| item . to_expr ( ) ) {
356
- if is_method_call ( expr) {
357
- self . context . force_one_line_chain . replace ( true ) ;
358
- }
379
+ match self . last_item ( ) {
380
+ Some ( OverflowableItem :: Expr ( expr) )
381
+ if !combine_arg_with_callee && is_method_call ( expr) =>
382
+ {
383
+ self . context . force_one_line_chain . replace ( true ) ;
359
384
}
385
+ _ => ( ) ,
360
386
}
361
387
let result = last_item_shape (
362
388
& self . items ,
@@ -596,12 +622,7 @@ fn last_item_shape(
596
622
shape : Shape ,
597
623
args_max_width : usize ,
598
624
) -> Option < Shape > {
599
- let is_nested_call = lists
600
- . iter ( )
601
- . next ( )
602
- . and_then ( |item| item. to_expr ( ) )
603
- . map_or ( false , is_nested_call) ;
604
- if items. len ( ) == 1 && !is_nested_call {
625
+ if items. len ( ) == 1 && !lists. iter ( ) . next ( ) ?. is_nested_call ( ) {
605
626
return Some ( shape) ;
606
627
}
607
628
let offset = items. iter ( ) . rev ( ) . skip ( 1 ) . fold ( 0 , |acc, i| {
0 commit comments