Skip to content

Commit 48e7103

Browse files
committed
Handle block comment before Traits in derive attribute (issue 4984)
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.
1 parent dd445ab commit 48e7103

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

src/lists.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,9 @@ where
570570
}
571571

572572
pub(crate) fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommentStyle) {
573-
let trimmed_pre_snippet = pre_snippet.trim();
573+
// "#[derive(" is included as part of the pre snippet if followed by a block comment
574+
// see https://github.com/rust-lang/rustfmt/issues/4984
575+
let trimmed_pre_snippet = pre_snippet.trim_start_matches("#[derive(").trim();
574576
// Both start and end are checked to support keeping a block comment inline with
575577
// the item, even if there are preceeding line comments, while still supporting
576578
// a snippet that starts with a block comment but also contains one or more
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[derive(/*Debug, */Clone)]
2+
struct Foo;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[derive(
2+
/* ---------- Some really important comment that just had to go inside the derive --------- */
3+
Debug, Clone, Eq, PartialEq,
4+
)]
5+
struct Foo {
6+
a: i32,
7+
b: T,
8+
}
9+
10+
#[derive(
11+
/*
12+
Some really important comment that just had to go inside the derive.
13+
Also had to be put over multiple lines
14+
*/
15+
Debug, Clone, Eq, PartialEq,
16+
)]
17+
struct Bar {
18+
a: i32,
19+
b: T,
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[derive(/*Debug, */ Clone)]
2+
struct Foo;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#[derive(
2+
/* ---------- Some really important comment that just had to go inside the derive --------- */
3+
Debug,
4+
Clone,
5+
Eq,
6+
PartialEq,
7+
)]
8+
struct Foo {
9+
a: i32,
10+
b: T,
11+
}
12+
13+
#[derive(
14+
/*
15+
Some really important comment that just had to go inside the derive.
16+
Also had to be put over multiple lines
17+
*/
18+
Debug,
19+
Clone,
20+
Eq,
21+
PartialEq,
22+
)]
23+
struct Bar {
24+
a: i32,
25+
b: T,
26+
}

0 commit comments

Comments
 (0)