Skip to content

Commit 40f4993

Browse files
ytmimicalebcartwright
authored andcommitted
Update derive attibute span to start after opening '('
Fixes 4984 When parsing derive attributes we're only concerned about the traits and comments listed between the opening and closing parentheses. Derive attribute spans currently start at the '#'. Span starts here | v #[derive(...)] After this update the derive spans start after the opening '('. Span starts here | V #[derive(...)]
1 parent f0f449d commit 40f4993

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

src/attr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting,
1313
use crate::overflow;
1414
use crate::rewrite::{Rewrite, RewriteContext};
1515
use crate::shape::Shape;
16+
use crate::source_map::SpanUtils;
1617
use crate::types::{rewrite_path, PathContext};
1718
use crate::utils::{count_newlines, mk_sp};
1819

@@ -116,7 +117,9 @@ fn format_derive(
116117
|span| span.lo(),
117118
|span| span.hi(),
118119
|span| Some(context.snippet(*span).to_owned()),
119-
attr.span.lo(),
120+
// We update derive attribute spans to start after the opening '('
121+
// This helps us focus parsing to just what's inside #[derive(...)]
122+
context.snippet_provider.span_after(attr.span, "("),
120123
attr.span.hi(),
121124
false,
122125
);
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)