]> git.lizzy.rs Git - rust.git/commitdiff
Fix an anomaly with comments and array literals
authorNick Cameron <ncameron@mozilla.com>
Tue, 17 Jul 2018 03:38:50 +0000 (15:38 +1200)
committerNick Cameron <ncameron@mozilla.com>
Tue, 17 Jul 2018 03:40:19 +0000 (15:40 +1200)
Closes #2842

src/lists.rs
tests/source/array_comment.rs [new file with mode: 0644]
tests/target/array_comment.rs [new file with mode: 0644]

index c415032ad67b9c5ab06f68d0cf22d3b93b5d2753..0e3baa7c1e15b034e6a0dbfb012a9900d6f3f394 100644 (file)
@@ -297,17 +297,15 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
                     if formatting.ends_with_newline {
                         trailing_separator = true;
                     }
+                } else if line_len > 0 {
+                    result.push(' ');
+                    line_len += 1;
                 }
 
                 if last && formatting.ends_with_newline {
                     separate = formatting.trailing_separator != SeparatorTactic::Never;
                 }
 
-                if line_len > 0 {
-                    result.push(' ');
-                    line_len += 1;
-                }
-
                 line_len += total_width;
             }
             _ => {}
@@ -341,6 +339,8 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
                     } else {
                         result.push('\n');
                         result.push_str(indent_str);
+                        // This is the width of the item (without comments).
+                        line_len = item.item.as_ref().map_or(0, |str| str.len());
                     }
                 } else {
                     result.push(' ');
diff --git a/tests/source/array_comment.rs b/tests/source/array_comment.rs
new file mode 100644 (file)
index 0000000..87372b2
--- /dev/null
@@ -0,0 +1,19 @@
+// Issue 2842
+// The comment should not make the last line shorter
+
+static XXX: [i8; 64] = [
+    1, // Comment
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+];
+
+static XXX: [i8; 64] = [
+    1,
+    // Comment
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+];
+
+static XXX: [i8; 64] = [
+    1,
+    // Comment
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+];
diff --git a/tests/target/array_comment.rs b/tests/target/array_comment.rs
new file mode 100644 (file)
index 0000000..93e1f5f
--- /dev/null
@@ -0,0 +1,18 @@
+// Issue 2842
+// The comment should not make the last line shorter
+
+static XXX: [i8; 64] = [
+    1, // Comment
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+];
+
+static XXX: [i8; 64] = [
+    1, // Comment
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+];
+
+static XXX: [i8; 64] = [
+    1, // Comment
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1,
+];