]> git.lizzy.rs Git - rust.git/commitdiff
Avoid rewriting the last argument whenever possible
authortopecongiro <seuchida@gmail.com>
Mon, 7 Aug 2017 08:30:32 +0000 (17:30 +0900)
committertopecongiro <seuchida@gmail.com>
Wed, 9 Aug 2017 07:37:21 +0000 (16:37 +0900)
src/expr.rs

index 1287b89ed636adc1c48d5579c98217fdda4207be..0fe6294d9aba5645739fdfff06b85251cb35679d 100644 (file)
@@ -2244,7 +2244,7 @@ fn try_overflow_last_arg<'a, T>(
 
     // Replace the last item with its first line to see if it fits with
     // first arguments.
-    let (orig_last, placeholder) = if overflow_last {
+    let placeholder = if overflow_last {
         let mut context = context.clone();
         if let Some(expr) = args[args.len() - 1].to_expr() {
             match expr.node {
@@ -2252,22 +2252,14 @@ fn try_overflow_last_arg<'a, T>(
                 _ => (),
             }
         }
-        last_arg_shape(&context, &item_vec, shape, args_max_width).map_or(
-            (None, None),
-            |arg_shape| {
-                rewrite_last_arg_with_overflow(
-                    &context,
-                    args,
-                    &mut item_vec[args.len() - 1],
-                    arg_shape,
-                )
-            },
-        )
+        last_arg_shape(&context, &item_vec, shape, args_max_width).and_then(|arg_shape| {
+            rewrite_last_arg_with_overflow(&context, args, &mut item_vec[args.len() - 1], arg_shape)
+        })
     } else {
-        (None, None)
+        None
     };
 
-    let tactic = definitive_tactic(
+    let mut tactic = definitive_tactic(
         &*item_vec,
         ListTactic::LimitedHorizontalVertical(args_max_width),
         Separator::Comma,
@@ -2280,10 +2272,17 @@ fn try_overflow_last_arg<'a, T>(
         (true, DefinitiveListTactic::Horizontal, placeholder @ Some(..)) => {
             item_vec[args.len() - 1].item = placeholder;
         }
-        (true, _, _) => {
-            item_vec[args.len() - 1].item = orig_last;
+        _ if args.len() >= 1 => {
+            item_vec[args.len() - 1].item = args.last()
+                .and_then(|last_arg| last_arg.rewrite(context, shape));
+            tactic = definitive_tactic(
+                &*item_vec,
+                ListTactic::LimitedHorizontalVertical(args_max_width),
+                Separator::Comma,
+                one_line_width,
+            );
         }
-        (false, _, _) => {}
+        _ => (),
     }
 
     tactic
@@ -2358,7 +2357,7 @@ fn rewrite_last_arg_with_overflow<'a, T>(
     args: &[&T],
     last_item: &mut ListItem,
     shape: Shape,
-) -> (Option<String>, Option<String>)
+) -> Option<String>
 where
     T: Rewrite + Spanned + ToExpr + 'a,
 {
@@ -2389,14 +2388,13 @@ fn rewrite_last_arg_with_overflow<'a, T>(
     } else {
         last_arg.rewrite(context, shape)
     };
-    let orig_last = last_item.item.clone();
 
     if let Some(rewrite) = rewrite {
         let rewrite_first_line = Some(rewrite[..first_line_width(&rewrite)].to_owned());
         last_item.item = rewrite_first_line;
-        (orig_last, Some(rewrite))
+        Some(rewrite)
     } else {
-        (orig_last, None)
+        None
     }
 }