diff --git a/src/items.rs b/src/items.rs index 4d82e192b7d..2e654d81e53 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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, }; @@ -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<'_>, @@ -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), @@ -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); } diff --git a/tests/source/issue-5507/issue-5507-version-one.rs b/tests/source/issue-5507/issue-5507-version-one.rs new file mode 100644 index 00000000000..6e7e8012d06 --- /dev/null +++ b/tests/source/issue-5507/issue-5507-version-one.rs @@ -0,0 +1,28 @@ +// rustfmt-version: One + +struct EmptyBody + where T: Eq { +} + +struct LineComment + where T: Eq { + // body +} + +struct MultiLineComment + where T: Eq { + /* + Multiline + comment. + */ +} + +struct BlockComment + where T: Eq { + /* block comment */ +} + +struct HasBody + where T: Eq { + x: T +} diff --git a/tests/source/issue-5507/issue-5507-version-two.rs b/tests/source/issue-5507/issue-5507-version-two.rs new file mode 100644 index 00000000000..6e1d7215749 --- /dev/null +++ b/tests/source/issue-5507/issue-5507-version-two.rs @@ -0,0 +1,28 @@ +// rustfmt-version: Two + +struct EmptyBody + where T: Eq { +} + +struct LineComment + where T: Eq { + // body +} + +struct MultiLineComment + where T: Eq { + /* + Multiline + comment. + */ +} + +struct BlockComment + where T: Eq { + /* block comment */ +} + +struct HasBody + where T: Eq { + x: T +} diff --git a/tests/target/issue-5507/issue-5507-version-one.rs b/tests/target/issue-5507/issue-5507-version-one.rs new file mode 100644 index 00000000000..be2860ef3e3 --- /dev/null +++ b/tests/target/issue-5507/issue-5507-version-one.rs @@ -0,0 +1,31 @@ +// rustfmt-version: One + +struct EmptyBody +where + T: Eq, {} + +struct LineComment +where + T: Eq, { + // body +} + +struct MultiLineComment +where + T: Eq, { + /* + Multiline + comment. + */ +} + +struct BlockComment +where + T: Eq, {/* block comment */} + +struct HasBody +where + T: Eq, +{ + x: T, +} diff --git a/tests/target/issue-5507/issue-5507-version-two.rs b/tests/target/issue-5507/issue-5507-version-two.rs new file mode 100644 index 00000000000..ea38108baf5 --- /dev/null +++ b/tests/target/issue-5507/issue-5507-version-two.rs @@ -0,0 +1,33 @@ +// rustfmt-version: Two + +struct EmptyBody +where + T: Eq, {} + +struct LineComment +where + T: Eq, +{ + // body +} + +struct MultiLineComment +where + T: Eq, +{ + /* + Multiline + comment. + */ +} + +struct BlockComment +where + T: Eq, {/* block comment */} + +struct HasBody +where + T: Eq, +{ + x: T, +}