-
Notifications
You must be signed in to change notification settings - Fork 933
Fix/Inconsistent Struct Body Opening Brace Placement After Where Clause #5508
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
0ee1308
2e46b4c
58e2a54
b3e3651
b5323ca
0c0fb58
254c829
088369e
f526382
c5098f8
5cd84e1
64a2321
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1262,6 +1262,29 @@ fn format_unit_struct( | |
Some(format!("{}{};", header_str, generics_str)) | ||
} | ||
|
||
fn set_brace_pos(has_fields: bool, has_content: bool, version: Version) -> BracePos { | ||
return match version { | ||
Version::One => { | ||
if has_fields { | ||
return BracePos::Auto; | ||
} | ||
BracePos::ForceSameLine | ||
} | ||
Version::Two => { | ||
match (has_fields, has_content) { | ||
// Doesn't have fields, there is nothing between {}, has generics. | ||
(true, true) => BracePos::ForceSameLine, | ||
|
||
// Doesn't have fields, has something between { }, has generics. | ||
(true, false) => BracePos::Auto, | ||
|
||
// Has fields, has generics. | ||
(false, _) => BracePos::Auto, | ||
} | ||
} | ||
}; | ||
} | ||
|
||
pub(crate) fn format_struct_struct( | ||
context: &RewriteContext<'_>, | ||
struct_parts: &StructParts<'_>, | ||
|
@@ -1289,11 +1312,11 @@ pub(crate) fn format_struct_struct( | |
context, | ||
g, | ||
context.config.brace_style(), | ||
if fields.is_empty() { | ||
BracePos::ForceSameLine | ||
} else { | ||
BracePos::Auto | ||
}, | ||
set_brace_pos( | ||
!fields.is_empty(), | ||
span.hi() <= body_lo + BytePos(5), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, this check if fields.is_empty() {
let snippet = context.snippet(mk_sp(body_lo, span.hi()));
let body_contains_commets = contains_comment(snippet);
if context.config.version() == Version::Two && body_contains_commets {
BracePos::Auto
} else {
BracePos::ForceSameLine
}
} else {
BracePos::Auto
} |
||
context.config.version(), | ||
), | ||
offset, | ||
// make a span that starts right after `struct Foo` | ||
mk_sp(header_hi, body_lo), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// rustfmt-version: Two | ||
|
||
struct EmptyBody<T> | ||
where T: Eq { | ||
} | ||
|
||
struct LineComment<T> | ||
where T: Eq { | ||
// body | ||
} | ||
|
||
struct BlockComment<T> | ||
where T: Eq { | ||
/* block comment */ | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also add a
|
||
|
||
struct HasBody<T> | ||
where T: Eq { | ||
x: T | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// rustfmt-indent_style: Visual | ||
// rustfmt-version: Two | ||
|
||
struct EmptyBody<T> | ||
where T: Eq { | ||
} | ||
|
||
struct LineComment<T> | ||
where T: Eq { | ||
// body | ||
} | ||
|
||
struct BlockComment<T> | ||
where T: Eq { | ||
/* block comment */ | ||
} | ||
|
||
struct HasBody<T> | ||
where T: Eq { | ||
x: T | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// rustfmt-version: Two | ||
|
||
struct EmptyBody<T> | ||
where | ||
T: Eq, | ||
{} | ||
|
||
struct LineComment<T> | ||
where | ||
T: Eq, | ||
{ | ||
// body | ||
} | ||
|
||
struct BlockComment<T> | ||
where | ||
T: Eq, | ||
{/* block comment */} | ||
|
||
struct HasBody<T> | ||
where | ||
T: Eq, | ||
{ | ||
x: T, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// rustfmt-indent_style: Visual | ||
// rustfmt-version: Two | ||
|
||
struct EmptyBody<T> | ||
where T: Eq | ||
{} | ||
|
||
struct LineComment<T> | ||
where T: Eq | ||
{ | ||
// body | ||
} | ||
|
||
struct BlockComment<T> | ||
where T: Eq | ||
{/* block comment */} | ||
|
||
struct HasBody<T> | ||
where T: Eq | ||
{ | ||
x: T, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of notes:
return match version
is redundant, since the match is the only statement in this function. remove the trailing semicolon from the match statement to convert it to an expression and have it implicitly return its value.