Skip to content

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
34 changes: 29 additions & 5 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,34 @@ fn format_unit_struct(
Some(format!("{}{};", header_str, generics_str))
}

fn set_brace_pos(
context: &RewriteContext<'_>,
struct_parts: &StructParts<'_>,
fields: &[ast::FieldDef],
body_lo: BytePos,
) -> BracePos {
if fields.is_empty() {
let span = struct_parts.span;
let generics_lo = if let Some(generics) = struct_parts.generics {
generics.span.lo()
} else {
body_lo
};
let snippet = context.snippet(mk_sp(generics_lo, span.hi()));
let body_contains_comments = contains_comment(snippet);
if context.config.version() == Version::Two {
return match (fields.is_empty(), body_contains_comments) {
(true, true) => BracePos::Auto,
(true, false) if struct_parts.generics.is_some() => BracePos::Auto,
(true, false) => BracePos::ForceSameLine,
(false, _) => BracePos::Auto,
};
}
return BracePos::ForceSameLine;
}
BracePos::Auto
}

pub(crate) fn format_struct_struct(
context: &RewriteContext<'_>,
struct_parts: &StructParts<'_>,
Expand Down Expand Up @@ -1289,11 +1317,7 @@ pub(crate) fn format_struct_struct(
context,
g,
context.config.brace_style(),
if fields.is_empty() {
BracePos::ForceSameLine
} else {
BracePos::Auto
},
set_brace_pos(&context, &struct_parts, &fields, body_lo),
offset,
// make a span that starts right after `struct Foo`
mk_sp(header_hi, body_lo),
Expand Down
28 changes: 28 additions & 0 deletions tests/source/issue-5507/issue-5507-version-one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// rustfmt-version: One

struct EmptyBody<T>
where T: Eq {
}

struct LineComment<T>
where T: Eq {
// body
}

struct MultiLineComment<T>
where T: Eq {
/*
Multiline
comment.
*/
}

struct BlockComment<T>
where T: Eq {
/* block comment */
}

struct HasBody<T>
where T: Eq {
x: T
}
28 changes: 28 additions & 0 deletions tests/source/issue-5507/issue-5507-version-two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// rustfmt-version: Two

struct EmptyBody<T>
where T: Eq {
}

struct LineComment<T>
where T: Eq {
// body
}

struct MultiLineComment<T>
where T: Eq {
/*
Multiline
comment.
*/
}

struct BlockComment<T>
where T: Eq {
/* block comment */
}

struct HasBody<T>
where T: Eq {
x: T
}
31 changes: 31 additions & 0 deletions tests/target/issue-5507/issue-5507-version-one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// rustfmt-version: One

struct EmptyBody<T>
where
T: Eq, {}

struct LineComment<T>
where
T: Eq, {
// body
}

struct MultiLineComment<T>
where
T: Eq, {
/*
Multiline
comment.
*/
}

struct BlockComment<T>
where
T: Eq, {/* block comment */}

struct HasBody<T>
where
T: Eq,
{
x: T,
}
35 changes: 35 additions & 0 deletions tests/target/issue-5507/issue-5507-version-two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// rustfmt-version: Two

struct EmptyBody<T>
where
T: Eq,
{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Subjective, but I think this should stay on the same line as the bound, Essentially following the version=One formatting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okok. I'll proceed to fix it


struct LineComment<T>
where
T: Eq,
{
// body
}

struct MultiLineComment<T>
where
T: Eq,
{
/*
Multiline
comment.
*/
}

struct BlockComment<T>
where
T: Eq,
{/* block comment */}
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, I think single line block comments should also follow the version=One formatting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a way to specifically check for block comments? If so, I the solution shouldn't be too complicated.

Copy link
Contributor

Choose a reason for hiding this comment

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

To my knowledge there isn't a standalone function that can be used for something like that. However you could check that using CommentStyle, which has a is_block_comment method. You'd use the comment_style function to construct one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ytmimi Done! Now empty impl blocks and impl blocks that only contain single line block comments have the same format on each version.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fantastic. Thank you for making those changes. I'll give this another review in the coming days!


struct HasBody<T>
where
T: Eq,
{
x: T,
}