about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYacin Tmimi <yacintmimi@gmail.com>2021-12-09 23:17:43 -0500
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2022-01-29 12:20:34 -0600
commit8b0b213cddb23a9bbe421b717d1a0e5fb3982712 (patch)
tree328e89d4cf0e2fd52e916c2f58a149e43bcaaa30
parentb4a4bf0bf8a16913b9f16f2aee7030065ff00931 (diff)
downloadrust-8b0b213cddb23a9bbe421b717d1a0e5fb3982712.tar.gz
rust-8b0b213cddb23a9bbe421b717d1a0e5fb3982712.zip
Prevent adding trailing whitespace when rewriting ast::Param
Fixes 5125

Previously, a newline was always added, even if the parameter name was
not preceded by any param attrs.

Now a newline is only added if there were param attrs.
-rw-r--r--src/items.rs8
-rw-r--r--tests/target/issue-5125/attributes_in_formal_fuction_parameter.rs6
-rw-r--r--tests/target/issue-5125/long_parameter_in_different_positions.rs24
-rw-r--r--tests/target/issue-5125/minimum_example.rs6
-rw-r--r--tests/target/issue-5125/with_leading_and_inline_comments.rs7
5 files changed, 50 insertions, 1 deletions
diff --git a/src/items.rs b/src/items.rs
index 4c7a33c86d2..007609aba24 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -2018,9 +2018,15 @@ impl Rewrite for ast::Param {
                 {
                     result.push_str(&ty_str);
                 } else {
+                    let prev_str = if param_attrs_result.is_empty() {
+                        param_attrs_result
+                    } else {
+                        param_attrs_result + &shape.to_string_with_newline(context.config)
+                    };
+
                     result = combine_strs_with_missing_comments(
                         context,
-                        &(param_attrs_result + &shape.to_string_with_newline(context.config)),
+                        &prev_str,
                         param_name,
                         span,
                         shape,
diff --git a/tests/target/issue-5125/attributes_in_formal_fuction_parameter.rs b/tests/target/issue-5125/attributes_in_formal_fuction_parameter.rs
new file mode 100644
index 00000000000..5d167932828
--- /dev/null
+++ b/tests/target/issue-5125/attributes_in_formal_fuction_parameter.rs
@@ -0,0 +1,6 @@
+fn foo(
+    #[unused] a: <u16 as intercom::type_system::ExternType<
+        intercom::type_system::AutomationTypeSystem,
+    >>::ForeignType,
+) {
+}
diff --git a/tests/target/issue-5125/long_parameter_in_different_positions.rs b/tests/target/issue-5125/long_parameter_in_different_positions.rs
new file mode 100644
index 00000000000..cab20381ce8
--- /dev/null
+++ b/tests/target/issue-5125/long_parameter_in_different_positions.rs
@@ -0,0 +1,24 @@
+fn middle(
+    a: usize,
+    b: <u16 as intercom::type_system::ExternType<
+        intercom::type_system::AutomationTypeSystem,
+    >>::ForeignType,
+    c: bool,
+) {
+}
+
+fn last(
+    a: usize,
+    b: <u16 as intercom::type_system::ExternType<
+        intercom::type_system::AutomationTypeSystem,
+    >>::ForeignType,
+) {
+}
+
+fn first(
+    a: <u16 as intercom::type_system::ExternType<
+        intercom::type_system::AutomationTypeSystem,
+    >>::ForeignType,
+    b: usize,
+) {
+}
diff --git a/tests/target/issue-5125/minimum_example.rs b/tests/target/issue-5125/minimum_example.rs
new file mode 100644
index 00000000000..8003e66968c
--- /dev/null
+++ b/tests/target/issue-5125/minimum_example.rs
@@ -0,0 +1,6 @@
+fn foo(
+    a: <u16 as intercom::type_system::ExternType<
+        intercom::type_system::AutomationTypeSystem,
+    >>::ForeignType,
+) {
+}
diff --git a/tests/target/issue-5125/with_leading_and_inline_comments.rs b/tests/target/issue-5125/with_leading_and_inline_comments.rs
new file mode 100644
index 00000000000..2340b2f3472
--- /dev/null
+++ b/tests/target/issue-5125/with_leading_and_inline_comments.rs
@@ -0,0 +1,7 @@
+fn foo(
+    // Pre Comment
+    a: <u16 as intercom::type_system::ExternType<
+        intercom::type_system::AutomationTypeSystem,
+    >>::ForeignType, // Inline comment
+) {
+}