Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix brace_style="PreferSameLine" with trailing_comma="Vertical" results #5556

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ fn lorem(ipsum: usize) {

fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div, {
T: Add + Sub + Mul + Div {
// body
}
```
Expand Down Expand Up @@ -290,7 +290,7 @@ struct Lorem {

struct Dolor<T>
where
T: Eq, {
T: Eq {
sit: T,
}
```
Expand Down
10 changes: 9 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2751,6 +2751,7 @@ fn rewrite_where_clause_rfc_style(
span_end: Option<BytePos>,
span_end_before_where: BytePos,
where_clause_option: WhereClauseOption,
brace_style: BraceStyle,
) -> Option<String> {
let (where_keyword, allow_single_line) = rewrite_where_keyword(
context,
Expand Down Expand Up @@ -2779,6 +2780,7 @@ fn rewrite_where_clause_rfc_style(
span_end,
where_clause_option,
force_single_line,
brace_style,
)?;

// 6 = `where `
Expand Down Expand Up @@ -2850,6 +2852,7 @@ fn rewrite_bounds_on_where_clause(
span_end: Option<BytePos>,
where_clause_option: WhereClauseOption,
force_single_line: bool,
brace_style: BraceStyle,
) -> Option<String> {
let span_start = predicates[0].span().lo();
// If we don't have the start of the next span, then use the end of the
Expand All @@ -2869,7 +2872,11 @@ fn rewrite_bounds_on_where_clause(
span_end,
false,
);
let comma_tactic = if where_clause_option.suppress_comma || force_single_line {
let comma_tactic = if where_clause_option.suppress_comma
|| force_single_line
|| brace_style == BraceStyle::PreferSameLine
&& context.config.trailing_comma() == SeparatorTactic::Vertical
{
SeparatorTactic::Never
} else {
context.config.trailing_comma()
Expand Down Expand Up @@ -2916,6 +2923,7 @@ fn rewrite_where_clause(
span_end,
span_end_before_where,
where_clause_option,
brace_style,
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/target/configs/brace_style/fn_prefer_same_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ fn lorem(ipsum: usize) {

fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div, {
T: Add + Sub + Mul + Div {
// body
}
2 changes: 1 addition & 1 deletion tests/target/configs/brace_style/item_prefer_same_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Lorem {

struct Dolor<T>
where
T: Eq, {
T: Eq {
sit: T,
}

Expand Down
8 changes: 4 additions & 4 deletions tests/target/fn-custom-6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn bar(

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb)
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
Copy link
Contributor

@ytmimi ytmimi Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically changing existing tests is not something we want to do and is usually a sign that the change needs to be version gated to prevent breaking formatting changes. However brace_style is an unstable option so the version gate might not be necessary in this case

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it if I have to! 👍

foo();
}

Expand All @@ -42,13 +42,13 @@ fn bar(
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee,
) where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
bar();
}

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
foo();
}

Expand All @@ -60,7 +60,7 @@ fn bar(
e: Eeeeeeeeeeeeeee,
) -> String
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
bar();
}

Expand Down
10 changes: 5 additions & 5 deletions tests/target/fn-custom-8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn bar(

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb)
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
foo();
}

Expand All @@ -42,13 +42,13 @@ fn bar(
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee,
) where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
bar();
}

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
foo();
}

Expand All @@ -60,7 +60,7 @@ fn bar(
e: Eeeeeeeeeeeeeee,
) -> String
where
T: UUUUUUUUUUU, {
T: UUUUUUUUUUU {
bar();
}

Expand All @@ -72,6 +72,6 @@ trait Test {
fn bar(a: u8) -> String
where
Foo: foooo,
Bar: barrr, {
Bar: barrr {
}
}
9 changes: 9 additions & 0 deletions tests/target/issue-2321/trailing_comma_always.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// rustfmt-brace_style: PreferSameLine
// rustfmt-trailing_comma: Always

fn lorem<S, T,>(lorem: S, ipsum: T,)
where
S: Add + Sub,
T: Mul + Div, {
// body
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also include a where clause with with a single bound. I like that you've included tests with trailing_comma: Always and trailing_comma: Vertical. Could you also expand the tests to include the other combinations of brace_style

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. I added test case for brace_style AlwaysNextLine and SameLineWhere pattern.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for including those additional test cases! I'll have some time later this week to give this a more thorough review 😁

9 changes: 9 additions & 0 deletions tests/target/issue-2321/trailing_comma_vertical.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// rustfmt-brace_style: PreferSameLine
// rustfmt-trailing_comma: Vertical

fn lorem<S, T>(lorem: S, ipsum: T)
where
S: Add + Sub,
T: Mul + Div {
// body
}
8 changes: 4 additions & 4 deletions tests/target/item-brace-style-prefer-same-line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ mod M {

enum A<T>
where
T: Copy, {
T: Copy {
A,
}

struct B<T>
where
T: Copy, {
T: Copy {
b: i32,
}

enum C<T>
where
T: Copy, {}
T: Copy {}

struct D<T>
where
T: Copy, {}
T: Copy {}
}