Skip to content

Fix corrupted comments in trait generics #4711

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 1 commit into
base: rustfmt-2.0.0-rc.2
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
11 changes: 6 additions & 5 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1192,11 +1192,12 @@ pub(crate) fn format_trait(
}
result.push_str(&where_clause_str);
}
let pre_block_span = if !generics.where_clause.predicates.is_empty() {
mk_sp(generics.where_clause.span.hi(), item.span.hi())
} else {
item.span
};
let pre_block_span =
if !generics.where_clause.predicates.is_empty() || !generics.params.is_empty() {
mk_sp(generics.where_clause.span.hi(), item.span.hi())
} else {
item.span
};
let pre_block_snippet = context.snippet(pre_block_span);
if let Some(lo) = pre_block_snippet.find('/') {
// 1 = `{`
Expand Down
22 changes: 22 additions & 0 deletions tests/source/issue-4643.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub trait Something<
A,
// some comment
B,
C
> {
fn a(&self, x: A) -> i32;
fn b(&self, x: B) -> i32;
fn c(&self, x: C) -> i32;
}

pub trait Something<
A,
// some comment
B,
// Another comment
C
> {
fn a(&self, x: A) -> i32;
fn b(&self, x: B) -> i32;
fn c(&self, x: C) -> i32;
}
24 changes: 24 additions & 0 deletions tests/target/issue-4643.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pub trait Something<
A,
// some comment
B,
C,
>
{
fn a(&self, x: A) -> i32;
fn b(&self, x: B) -> i32;
fn c(&self, x: C) -> i32;
}

pub trait Something<
A,
// some comment
B,
// Another comment
C,
>
{
fn a(&self, x: A) -> i32;
fn b(&self, x: B) -> i32;
fn c(&self, x: C) -> i32;
}