about summary refs log tree commit diff
diff options
context:
space:
mode:
authorIVIURRAY <haydn.murray1@yahoo.co.uk>2024-01-08 15:06:25 +0000
committerGitHub <noreply@github.com>2024-01-08 10:06:25 -0500
commit6356fca675bd756d71f5c123cd053d17b16c573e (patch)
tree235f7125f12c5f1a629fd6b195af30e9f6ffdf21
parent75e3172aaaff1ff6be42f36777a6814c0a72320f (diff)
downloadrust-6356fca675bd756d71f5c123cd053d17b16c573e.tar.gz
rust-6356fca675bd756d71f5c123cd053d17b16c573e.zip
Prevent enum variant attributes from wrapping one character early
Fixes 5801 when using `version=Two`

Previously, doc comments would wrap one character less than the
`comment_width` when using `wrap_comments=true`, and fn-like attributes
would wrap one character before the `attr_fn_like_width`.

Now, when using `version=Two` enum variant attributes won't wrap early
-rw-r--r--src/items.rs13
-rw-r--r--tests/config/issue-5801-v1.toml3
-rw-r--r--tests/config/issue-5801-v2.toml3
-rw-r--r--tests/source/issue-5801/attribute_unexpectedly_wraps_before_max_width.rs8
-rw-r--r--tests/source/issue-5801/comment_unexpectedly_wraps_before_max_width.rs17
-rw-r--r--tests/target/issue-5801/attribute_does_not_wrap_within_max_width.rs8
-rw-r--r--tests/target/issue-5801/attribute_unexpectedly_wraps_before_max_width.rs16
-rw-r--r--tests/target/issue-5801/comment_does_not_wrap_within_max_width.rs16
-rw-r--r--tests/target/issue-5801/comment_unexpectedly_wraps_before_max_width.rs18
9 files changed, 99 insertions, 3 deletions
diff --git a/src/items.rs b/src/items.rs
index 0bcfa3f448b..c6976324c71 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -656,9 +656,16 @@ impl<'a> FmtVisitor<'a> {
         }
 
         let context = self.get_context();
-        // 1 = ','
-        let shape = self.shape().sub_width(1)?;
-        let attrs_str = field.attrs.rewrite(&context, shape)?;
+        let shape = self.shape();
+        let attrs_str = if context.config.version() == Version::Two {
+            field.attrs.rewrite(&context, shape)?
+        } else {
+            // Version::One formatting that was off by 1. See issue #5801
+            field.attrs.rewrite(&context, shape.sub_width(1)?)?
+        };
+        // sub_width(1) to take the trailing comma into account
+        let shape = shape.sub_width(1)?;
+
         let lo = field
             .attrs
             .last()
diff --git a/tests/config/issue-5801-v1.toml b/tests/config/issue-5801-v1.toml
new file mode 100644
index 00000000000..8695df0099e
--- /dev/null
+++ b/tests/config/issue-5801-v1.toml
@@ -0,0 +1,3 @@
+max_width = 120
+version = "One"
+attr_fn_like_width = 120
\ No newline at end of file
diff --git a/tests/config/issue-5801-v2.toml b/tests/config/issue-5801-v2.toml
new file mode 100644
index 00000000000..948f4fb66bf
--- /dev/null
+++ b/tests/config/issue-5801-v2.toml
@@ -0,0 +1,3 @@
+max_width = 120
+version = "Two"
+attr_fn_like_width = 120
\ No newline at end of file
diff --git a/tests/source/issue-5801/attribute_unexpectedly_wraps_before_max_width.rs b/tests/source/issue-5801/attribute_unexpectedly_wraps_before_max_width.rs
new file mode 100644
index 00000000000..59902c7bcce
--- /dev/null
+++ b/tests/source/issue-5801/attribute_unexpectedly_wraps_before_max_width.rs
@@ -0,0 +1,8 @@
+// rustfmt-config: issue-5801-v1.toml
+
+pub enum Severity {
+    #[something(AAAAAAAAAAAAA, BBBBBBBBBBBBBB, CCCCCCCCCCCCCCCC, DDDDDDDDDDDDD, EEEEEEEEEEEE, FFFFFFFFFFF, GGGGGGGGGGG)]
+    AttrsWillWrap,
+    #[something_else(hhhhhhhhhhhhhhhh, iiiiiiiiiiiiiiii, jjjjjjjjjjjjjjj, kkkkkkkkkkkkk, llllllllllll, mmmmmmmmmmmmmm)]
+    AttrsWontWrap,
+}
diff --git a/tests/source/issue-5801/comment_unexpectedly_wraps_before_max_width.rs b/tests/source/issue-5801/comment_unexpectedly_wraps_before_max_width.rs
new file mode 100644
index 00000000000..5847afd9560
--- /dev/null
+++ b/tests/source/issue-5801/comment_unexpectedly_wraps_before_max_width.rs
@@ -0,0 +1,17 @@
+// rustfmt-comment_width: 120
+// rustfmt-wrap_comments: true
+// rustfmt-max_width: 120
+// rustfmt-version: One
+
+/// This function is 120 columns wide and is left alone. This comment is 120 columns wide and the formatter is also fine
+fn my_super_cool_function_name(my_very_cool_argument_name: String, my_other_very_cool_argument_name: String) -> String {
+    unimplemented!()
+}
+
+pub enum Severity {
+    /// In version one, the below line got wrapped prematurely as we subtracted 1 to account for `,`. See issue #5801.
+    /// But here, this comment is 120 columns wide and the formatter wants to split it up onto two separate lines still.
+    Error,
+    /// This comment is 119 columns wide and works perfectly. Lorem ipsum. lorem ipsum. lorem ipsum. lorem ipsum lorem.
+    Warning,
+}
diff --git a/tests/target/issue-5801/attribute_does_not_wrap_within_max_width.rs b/tests/target/issue-5801/attribute_does_not_wrap_within_max_width.rs
new file mode 100644
index 00000000000..e3b6f3b37f5
--- /dev/null
+++ b/tests/target/issue-5801/attribute_does_not_wrap_within_max_width.rs
@@ -0,0 +1,8 @@
+// rustfmt-config: issue-5801-v2.toml
+
+pub enum Severity {
+    #[something(AAAAAAAAAAAAA, BBBBBBBBBBBBBB, CCCCCCCCCCCCCCCC, DDDDDDDDDDDDD, EEEEEEEEEEEE, FFFFFFFFFFF, GGGGGGGGGGG)]
+    AttrsWillWrap,
+    #[something_else(hhhhhhhhhhhhhhhh, iiiiiiiiiiiiiiii, jjjjjjjjjjjjjjj, kkkkkkkkkkkkk, llllllllllll, mmmmmmmmmmmmmm)]
+    AttrsWontWrap,
+}
diff --git a/tests/target/issue-5801/attribute_unexpectedly_wraps_before_max_width.rs b/tests/target/issue-5801/attribute_unexpectedly_wraps_before_max_width.rs
new file mode 100644
index 00000000000..89676666225
--- /dev/null
+++ b/tests/target/issue-5801/attribute_unexpectedly_wraps_before_max_width.rs
@@ -0,0 +1,16 @@
+// rustfmt-config: issue-5801-v1.toml
+
+pub enum Severity {
+    #[something(
+        AAAAAAAAAAAAA,
+        BBBBBBBBBBBBBB,
+        CCCCCCCCCCCCCCCC,
+        DDDDDDDDDDDDD,
+        EEEEEEEEEEEE,
+        FFFFFFFFFFF,
+        GGGGGGGGGGG
+    )]
+    AttrsWillWrap,
+    #[something_else(hhhhhhhhhhhhhhhh, iiiiiiiiiiiiiiii, jjjjjjjjjjjjjjj, kkkkkkkkkkkkk, llllllllllll, mmmmmmmmmmmmmm)]
+    AttrsWontWrap,
+}
diff --git a/tests/target/issue-5801/comment_does_not_wrap_within_max_width.rs b/tests/target/issue-5801/comment_does_not_wrap_within_max_width.rs
new file mode 100644
index 00000000000..9f294751108
--- /dev/null
+++ b/tests/target/issue-5801/comment_does_not_wrap_within_max_width.rs
@@ -0,0 +1,16 @@
+// rustfmt-comment_width: 120
+// rustfmt-wrap_comments: true
+// rustfmt-max_width: 120
+// rustfmt-version: Two
+
+/// This function is 120 columns wide and is left alone. This comment is 120 columns wide and the formatter is also fine
+fn my_super_cool_function_name(my_very_cool_argument_name: String, my_other_very_cool_argument_name: String) -> String {
+    unimplemented!()
+}
+
+pub enum Severity {
+    /// But here, this comment is 120 columns wide and the formatter wants to split it up onto two separate lines still.
+    Error,
+    /// This comment is 119 columns wide and works perfectly. Lorem ipsum. lorem ipsum. lorem ipsum. lorem ipsum lorem.
+    Warning,
+}
diff --git a/tests/target/issue-5801/comment_unexpectedly_wraps_before_max_width.rs b/tests/target/issue-5801/comment_unexpectedly_wraps_before_max_width.rs
new file mode 100644
index 00000000000..dd839dd4548
--- /dev/null
+++ b/tests/target/issue-5801/comment_unexpectedly_wraps_before_max_width.rs
@@ -0,0 +1,18 @@
+// rustfmt-comment_width: 120
+// rustfmt-wrap_comments: true
+// rustfmt-max_width: 120
+// rustfmt-version: One
+
+/// This function is 120 columns wide and is left alone. This comment is 120 columns wide and the formatter is also fine
+fn my_super_cool_function_name(my_very_cool_argument_name: String, my_other_very_cool_argument_name: String) -> String {
+    unimplemented!()
+}
+
+pub enum Severity {
+    /// In version one, the below line got wrapped prematurely as we subtracted 1 to account for `,`. See issue #5801.
+    /// But here, this comment is 120 columns wide and the formatter wants to split it up onto two separate lines
+    /// still.
+    Error,
+    /// This comment is 119 columns wide and works perfectly. Lorem ipsum. lorem ipsum. lorem ipsum. lorem ipsum lorem.
+    Warning,
+}