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/Inconsistent Struct Body Opening Brace Placement After Where Clause #5508

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 27 additions & 7 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_span::{symbol, BytePos, Span, DUMMY_SP};

use crate::attr::filter_inline_attrs;
use crate::comment::{
combine_strs_with_missing_comments, contains_comment, is_last_comment_block,
combine_strs_with_missing_comments, comment_style, contains_comment, is_last_comment_block,
recover_comment_removed, recover_missing_comment_in_span, rewrite_missing_comment,
FindUncommented,
};
Expand Down Expand Up @@ -1375,6 +1375,30 @@ fn format_unit_struct(
Some(format!("{header_str}{generics_str};"))
}

fn set_struct_brace_pos(
context: &RewriteContext<'_>,
fields: &[ast::FieldDef],
span: Span,
) -> BracePos {
if !fields.is_empty() {
return BracePos::Auto;
}
let snippet = context.snippet(span).trim();

if snippet.is_empty() || context.config.version() == Version::One {
return BracePos::ForceSameLine;
}

let is_single_line = is_single_line(snippet);
let is_block_comment = comment_style(snippet, false).is_block_comment();

if is_single_line && is_block_comment {
BracePos::ForceSameLine
} else {
BracePos::Auto
}
}

pub(crate) fn format_struct_struct(
context: &RewriteContext<'_>,
struct_parts: &StructParts<'_>,
Expand All @@ -1397,16 +1421,13 @@ pub(crate) fn format_struct_struct(
context.snippet_provider.span_after(span, "{")
};

let inner_span = mk_sp(body_lo, span.hi() - BytePos(1));
let generics_str = match struct_parts.generics {
Some(g) => format_generics(
context,
g,
context.config.brace_style(),
if fields.is_empty() {
BracePos::ForceSameLine
} else {
BracePos::Auto
},
set_struct_brace_pos(&context, &fields, inner_span),
offset,
// make a span that starts right after `struct Foo`
mk_sp(header_hi, body_lo),
Expand Down Expand Up @@ -1439,7 +1460,6 @@ pub(crate) fn format_struct_struct(
}

if fields.is_empty() {
let inner_span = mk_sp(body_lo, span.hi() - BytePos(1));
format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "", "}");
return Some(result);
}
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,
}
33 changes: 33 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,33 @@
// 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,
}