Skip to content

Commit

Permalink
Handle block comment before Traits in derive attribute (issue 4984)
Browse files Browse the repository at this point in the history
When a block comment comes before the traits of a derive attribute
the string "#[derive(" is included as part of the the pre snippet,
and as a result gets duplicated.

For example:

    #[derive(/*Debug, */Clone)] -> #[derive(#[derive(/*Debug, */ Clone)]

Now the string "#[derive(" is trimmed from the start of the pre snippet
before looking for the pre comment.
  • Loading branch information
ytmimi committed Sep 24, 2021
1 parent dd445ab commit 48e7103
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,9 @@ where
}

pub(crate) fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommentStyle) {
let trimmed_pre_snippet = pre_snippet.trim();
// "#[derive(" is included as part of the pre snippet if followed by a block comment
// see https://github.com/rust-lang/rustfmt/issues/4984
let trimmed_pre_snippet = pre_snippet.trim_start_matches("#[derive(").trim();
// Both start and end are checked to support keeping a block comment inline with
// the item, even if there are preceeding line comments, while still supporting
// a snippet that starts with a block comment but also contains one or more
Expand Down
2 changes: 2 additions & 0 deletions tests/source/issue-4984/minimum_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[derive(/*Debug, */Clone)]
struct Foo;
20 changes: 20 additions & 0 deletions tests/source/issue-4984/multi_line_derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[derive(
/* ---------- Some really important comment that just had to go inside the derive --------- */
Debug, Clone, Eq, PartialEq,
)]
struct Foo {
a: i32,
b: T,
}

#[derive(
/*
Some really important comment that just had to go inside the derive.
Also had to be put over multiple lines
*/
Debug, Clone, Eq, PartialEq,
)]
struct Bar {
a: i32,
b: T,
}
2 changes: 2 additions & 0 deletions tests/target/issue-4984/minimum_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[derive(/*Debug, */ Clone)]
struct Foo;
26 changes: 26 additions & 0 deletions tests/target/issue-4984/multi_line_derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[derive(
/* ---------- Some really important comment that just had to go inside the derive --------- */
Debug,
Clone,
Eq,
PartialEq,
)]
struct Foo {
a: i32,
b: T,
}

#[derive(
/*
Some really important comment that just had to go inside the derive.
Also had to be put over multiple lines
*/
Debug,
Clone,
Eq,
PartialEq,
)]
struct Bar {
a: i32,
b: T,
}

0 comments on commit 48e7103

Please sign in to comment.