]> git.lizzy.rs Git - rust.git/blobdiff - src/pairs.rs
Do not combine short parent and comment
[rust.git] / src / pairs.rs
index 3cd044d606e147c34bc4b998c4d0bdcc86404ded..17b869750226c05967892dea4304edb7005d267d 100644 (file)
@@ -69,10 +69,7 @@ fn rewrite_pairs_one_line<T: Rewrite>(
     let mut result = String::new();
     let base_shape = shape.block();
 
-    for (e, s) in list.list[..list.list.len()]
-        .iter()
-        .zip(list.separators.iter())
-    {
+    for (e, s) in list.list.iter().zip(list.separators.iter()) {
         let cur_shape = base_shape.offset_left(last_line_width(&result))?;
         let rewrite = e.rewrite(context, cur_shape)?;
 
@@ -128,7 +125,7 @@ fn rewrite_pairs_multiline<T: Rewrite>(
         IndentStyle::Visual => shape.visual_indent(0),
         IndentStyle::Block => shape.block_indent(context.config.tab_spaces()),
     }).with_max_width(&context.config)
-        .sub_width(rhs_offset)?;
+    .sub_width(rhs_offset)?;
 
     let indent_str = nested_shape.indent.to_string_with_newline(context.config);
     let mut result = String::new();
@@ -137,7 +134,15 @@ fn rewrite_pairs_multiline<T: Rewrite>(
     result.push_str(&rewrite);
 
     for (e, s) in list.list[1..].iter().zip(list.separators.iter()) {
-        if trimmed_last_line_width(&result) <= context.config.tab_spaces() {
+        // The following test checks if we should keep two subexprs on the same
+        // line. We do this if not doing so would create an orphan and there is
+        // enough space to do so.
+        let offset = if result.contains('\n') {
+            0
+        } else {
+            shape.used_width()
+        };
+        if last_line_width(&result) + offset <= nested_shape.used_width() {
             // We must snuggle the next line onto the previous line to avoid an orphan.
             if let Some(line_shape) =
                 shape.offset_left(s.len() + 2 + trimmed_last_line_width(&result))