rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

[unstable option] where_single_line

Open scampi opened this issue 6 years ago • 8 comments
trafficstars

Tracking issue for unstable option: where_single_line

scampi avatar Feb 13 '19 22:02 scampi

Is this supposed to allow multiple bounds on a single line (similar to how function arguments, struct fields etc. are only split if they won't fit on a single line)?

Here's an example diff from applying rustfmt to Rand which looks kind of weird (splits one already short line, and collapses a block into a single, longer line):

@@ -207,13 +235,11 @@ pub trait Distribution<T> {
     /// }
     /// ```
     fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
-    where R: Rng, Self: Sized
+    where
+        R: Rng,
+        Self: Sized,
     {
-        DistIter {
-            distr: self,
-            rng,
-            phantom: ::core::marker::PhantomData,
-        }
+        DistIter { distr: self, rng, phantom: ::core::marker::PhantomData }
     }
 }

dhardy avatar Jan 01 '20 13:01 dhardy

With this option turned on, I think the following change to the where condition should not have happened? Or is this a different case since the where directly follows a )?

        fn test_samples<T: SampleUniform + Copy + core::fmt::Debug + PartialEq>(
-            lb: T, ub: T, expected_single: &[T], expected_multiple: &[T]
-        )
-        where Uniform<T>: Distribution<T>
+            lb: T,
+            ub: T,
+            expected_single: &[T],
+            expected_multiple: &[T],
+        ) where
+            Uniform<T>: Distribution<T>,
         {

dhardy avatar Jan 01 '20 14:01 dhardy

I'm also struggling to figure out what this option actually does. I expected these bounds to all be on the same line:

 impl<S, T, I, O, Ix> Machine for DfaMachine<S, T, I, O, Ix>
-where I: Iterator, O: Iterator, Ix: IndexType,
+where
+    I: Iterator,
+    O: Iterator,
+    Ix: IndexType,
 {
     type State = S;
     type Transition = T;

CJKay avatar May 20 '22 12:05 CJKay

Thank you both for sharing, and apologies @dhardy for missing the prior questions (suspect it got list in the github inbox noise). I'm not sure offhand if the behavior shared above is a bug or if we've simply got spectacularly underspecified docs for the option, but I vaguely remember having a similar thought/question in the past over the formatting of a type alias.

calebcartwright avatar May 20 '22 13:05 calebcartwright

I've been using this option for a while under the impression that it means: "if there is only a single where condition, put it on one line" otherwise format it like normal.

I don't want to speak on behalf of the author, but this is the behavior I've consistently observed from it, and honestly, is the behavior I personally want.

I think supporting a 3rd option (that always puts all clauses on one line), and updating the docs might go a long way here.

InsertCreativityHere avatar Mar 11 '24 21:03 InsertCreativityHere

I think it would look neater to have it based on length rather than just if there's one, as stacking two pretty short bounds on top of each other looks less than ideal to me.

I also wonder how this could interact with brace_style. I would personally want it to be on the same line if the where clause is on a single line, but on the next line of the where clause is expanded

KaitlynEthylia avatar May 05 '24 21:05 KaitlynEthylia