about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYacin Tmimi <ytmimi@horizonmedia.com>2021-10-14 17:16:28 -0400
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2021-10-17 16:27:05 -0500
commit5f4811ed7bc600e0cbe40c962e8933adb9baaddf (patch)
treeb21523a63c23b31f38ebcc9cc93a949976b5d0d5
parent0cff306b61922ed5f460a4f6cbd4134498a3c42f (diff)
downloadrust-5f4811ed7bc600e0cbe40c962e8933adb9baaddf.tar.gz
rust-5f4811ed7bc600e0cbe40c962e8933adb9baaddf.zip
Handle DefinitiveListTactic::SpecialMacro when writing pre-comments
Resolves 4615

Previously only Vertical and Mixed enum variants of DefinitiveListTactic
were considered when rewriting pre-comments for inner items in
lists::write_list.

Because we failed to considering the SpecialMacro variant we ended up in
a scenario where a ListItem with a pre_comment and a pre_comment_style
of ListItemCommentStyle::DifferentLine was written on the same line as the
list item itself.

Now we apply the same pre-comment formatting to SpecialMacro, Vertical,
and Mixed variants of DefinitiveListTactic.
-rw-r--r--src/lists.rs46
-rw-r--r--tests/source/issue-4615/minimum_example.rs4
-rw-r--r--tests/target/issue-4615/minimum_example.rs5
3 files changed, 33 insertions, 22 deletions
diff --git a/src/lists.rs b/src/lists.rs
index 73e886c5563..f0c2ae1499f 100644
--- a/src/lists.rs
+++ b/src/lists.rs
@@ -367,29 +367,31 @@ where
             result.push_str(&comment);
 
             if !inner_item.is_empty() {
-                if tactic == DefinitiveListTactic::Vertical || tactic == DefinitiveListTactic::Mixed
-                {
-                    // We cannot keep pre-comments on the same line if the comment if normalized.
-                    let keep_comment = if formatting.config.normalize_comments()
-                        || item.pre_comment_style == ListItemCommentStyle::DifferentLine
-                    {
-                        false
-                    } else {
-                        // We will try to keep the comment on the same line with the item here.
-                        // 1 = ` `
-                        let total_width = total_item_width(item) + item_sep_len + 1;
-                        total_width <= formatting.shape.width
-                    };
-                    if keep_comment {
-                        result.push(' ');
-                    } 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, |s| unicode_str_width(&s));
+                match tactic {
+                    DefinitiveListTactic::SpecialMacro(_)
+                    | DefinitiveListTactic::Vertical
+                    | DefinitiveListTactic::Mixed => {
+                        // We cannot keep pre-comments on the same line if the comment is normalized
+                        let keep_comment = if formatting.config.normalize_comments()
+                            || item.pre_comment_style == ListItemCommentStyle::DifferentLine
+                        {
+                            false
+                        } else {
+                            // We will try to keep the comment on the same line with the item here.
+                            // 1 = ` `
+                            let total_width = total_item_width(item) + item_sep_len + 1;
+                            total_width <= formatting.shape.width
+                        };
+                        if keep_comment {
+                            result.push(' ');
+                        } 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, |s| unicode_str_width(&s));
+                        }
                     }
-                } else {
-                    result.push(' ');
+                    _ => result.push(' '),
                 }
             }
             item_max_width = None;
diff --git a/tests/source/issue-4615/minimum_example.rs b/tests/source/issue-4615/minimum_example.rs
new file mode 100644
index 00000000000..89af5d1239d
--- /dev/null
+++ b/tests/source/issue-4615/minimum_example.rs
@@ -0,0 +1,4 @@
+info!(//debug
+    "{}: sending function_code={:04x} data={:04x} crc=0x{:04X} data={:02X?}",
+    self.name, function_code, data, crc, output_cmd
+);
diff --git a/tests/target/issue-4615/minimum_example.rs b/tests/target/issue-4615/minimum_example.rs
new file mode 100644
index 00000000000..223b89b812d
--- /dev/null
+++ b/tests/target/issue-4615/minimum_example.rs
@@ -0,0 +1,5 @@
+info!(
+    //debug
+    "{}: sending function_code={:04x} data={:04x} crc=0x{:04X} data={:02X?}",
+    self.name, function_code, data, crc, output_cmd
+);