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 9 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
61 changes: 53 additions & 8 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ 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,
recover_comment_removed, recover_missing_comment_in_span, rewrite_missing_comment,
FindUncommented,
combine_strs_with_missing_comments, comment_style, contains_comment, find_comment_end,
is_last_comment_block, recover_comment_removed, recover_missing_comment_in_span,
rewrite_missing_comment, FindUncommented,
};
use crate::config::lists::*;
use crate::config::{BraceStyle, Config, IndentStyle, Version};
Expand Down Expand Up @@ -1262,6 +1262,55 @@ 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);
let has_single_line_block_comment = if body_contains_comments {
let comment_start = match snippet.find('/') {
Some(i) => i,
None => 0,
};
let comment_snippet = &snippet[comment_start..];
let comment = comment_style(comment_snippet, false);
let is_block_comment = comment.is_block_comment();
let is_single_line_comment = if is_block_comment {
let comment_end = match find_comment_end(comment_snippet) {
Some(i) => i,
None => comment_start,
};
is_single_line(&comment_snippet[..=comment_end - 1])
} else {
false
};
is_single_line_comment
} else {
false
};

if context.config.version() == Version::Two {
return match body_contains_comments {
true if has_single_line_block_comment => BracePos::ForceSameLine,
true => BracePos::Auto,
false => BracePos::ForceSameLine,
};
}
return BracePos::ForceSameLine;
}
BracePos::Auto
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I took a look, and I think we could simplify the logic here as follows:

Note: I renamed set_brace_pos to set_struct_brace_pos to be more explicit.

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

    // At this point we know there aren't any struct fields
    // This snippet contains the unformatted, trimmed struct body
    let snippet = context.snippet(span).trim();

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

    // The body isn't empty so there must be comments.
    // we also know that we're implicitly using Version Two formatting.
    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
    }
}

At the call site we can hoist inner_span from line 1329 so we can use that as the span argument to set_struct_brace_pos. Let me know if all that makes sense.


pub(crate) fn format_struct_struct(
context: &RewriteContext<'_>,
struct_parts: &StructParts<'_>,
Expand Down Expand Up @@ -1289,11 +1338,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,
}
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,
}