about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYacin Tmimi <yacintmimi@gmail.com>2021-10-08 19:22:12 -0400
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2021-10-30 23:07:34 -0500
commit5ce82e15133501cdfa96d58370e9b4c3873546b1 (patch)
tree66a57ada679af9ee0fe0499ffbf8ea448a1855e7
parenta4d7011c18eeae7fbb11286822b9519dc9a9325f (diff)
downloadrust-5ce82e15133501cdfa96d58370e9b4c3873546b1.tar.gz
rust-5ce82e15133501cdfa96d58370e9b4c3873546b1.zip
Prevent trailing whitespace in where clause bound predicate
resolves 5012
resolves 4850

This behavior was noticed when using the ``trailing_comma = "Never"``
configuration option (5012).

This behavior was also noticed when using default configurations (4850).

rustfmt would add a trailing space to where clause bounds that had an
empty right hand side.

Now no trailing space is added to the end of these where clause bounds.
-rw-r--r--src/expr.rs3
-rw-r--r--tests/target/issue-5012/trailing_comma_always.rs8
-rw-r--r--tests/target/issue-5012/trailing_comma_never.rs8
-rw-r--r--tests/target/issue_4850.rs4
4 files changed, 23 insertions, 0 deletions
diff --git a/src/expr.rs b/src/expr.rs
index 1ca01f9db9a..58942e442de 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -1962,6 +1962,9 @@ fn choose_rhs<R: Rewrite>(
     has_rhs_comment: bool,
 ) -> Option<String> {
     match orig_rhs {
+        Some(ref new_str) if new_str.is_empty() => {
+            return Some(String::new());
+        }
         Some(ref new_str)
             if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width =>
         {
diff --git a/tests/target/issue-5012/trailing_comma_always.rs b/tests/target/issue-5012/trailing_comma_always.rs
new file mode 100644
index 00000000000..ff9c40fbbd8
--- /dev/null
+++ b/tests/target/issue-5012/trailing_comma_always.rs
@@ -0,0 +1,8 @@
+// rustfmt-trailing_comma: Always
+
+pub struct Matrix<T, const R: usize, const C: usize,>
+where
+    [T; R * C]:,
+{
+    contents: [T; R * C],
+}
diff --git a/tests/target/issue-5012/trailing_comma_never.rs b/tests/target/issue-5012/trailing_comma_never.rs
new file mode 100644
index 00000000000..2fac8eae52b
--- /dev/null
+++ b/tests/target/issue-5012/trailing_comma_never.rs
@@ -0,0 +1,8 @@
+// rustfmt-trailing_comma: Never
+
+pub struct Matrix<T, const R: usize, const C: usize>
+where
+    [T; R * C]:
+{
+    contents: [T; R * C]
+}
diff --git a/tests/target/issue_4850.rs b/tests/target/issue_4850.rs
new file mode 100644
index 00000000000..7d4da9022fe
--- /dev/null
+++ b/tests/target/issue_4850.rs
@@ -0,0 +1,4 @@
+impl ThisIsALongStructNameToPushTheWhereToWrapLolololol where
+    [(); this_is_a_long_const_function_name()]:
+{
+}