Skip to content

Commit 6877e6e

Browse files
bors[bot]matklad
andauthored
Merge #8578
8578: fix: false positive about inner attrs in docs r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 6991b51 + 5f89a60 commit 6877e6e

File tree

6 files changed

+238
-182
lines changed

6 files changed

+238
-182
lines changed

crates/hir_def/src/attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ fn inner_attributes(
545545
_ => return None,
546546
}
547547
};
548-
let attrs = attrs.filter(|attr| attr.excl_token().is_some());
548+
let attrs = attrs.filter(|attr| attr.kind().is_inner());
549549
let docs = docs.filter(|doc| doc.is_inner());
550550
Some((attrs, docs))
551551
}
@@ -740,7 +740,7 @@ fn collect_attrs(
740740
let (inner_attrs, inner_docs) = inner_attributes(owner.syntax())
741741
.map_or((None, None), |(attrs, docs)| (Some(attrs), Some(docs)));
742742

743-
let outer_attrs = owner.attrs().filter(|attr| attr.excl_token().is_none());
743+
let outer_attrs = owner.attrs().filter(|attr| attr.kind().is_outer());
744744
let attrs = outer_attrs
745745
.chain(inner_attrs.into_iter().flatten())
746746
.map(|attr| (attr.syntax().text_range().start(), Either::Left(attr)));

crates/syntax/src/ast/node_ext.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ pub enum AttrKind {
125125
Outer,
126126
}
127127

128+
impl AttrKind {
129+
/// Returns `true` if the attr_kind is [`Inner`].
130+
pub fn is_inner(&self) -> bool {
131+
matches!(self, Self::Inner)
132+
}
133+
134+
/// Returns `true` if the attr_kind is [`Outer`].
135+
pub fn is_outer(&self) -> bool {
136+
matches!(self, Self::Outer)
137+
}
138+
}
139+
128140
impl ast::Attr {
129141
pub fn as_simple_atom(&self) -> Option<SmolStr> {
130142
if self.eq_token().is_some() || self.token_tree().is_some() {

crates/syntax/src/validation/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) fn validate_block_expr(block: ast::BlockExpr, errors: &mut Vec<Syntax
1313
_ => {}
1414
}
1515
}
16-
errors.extend(block.attrs().map(|attr| {
16+
errors.extend(block.attrs().filter(|attr| attr.kind().is_inner()).map(|attr| {
1717
SyntaxError::new(
1818
"A block in this position cannot accept inner attributes",
1919
attr.syntax().text_range(),
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
[email protected] "\"Inner attributes all ..."
25+
26+
27+
28+
[email protected] "//! As are ModuleDoc ..."
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
[email protected] "\"Inner attributes are ..."
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
[email protected] "\"Being validated is n ..."
59+
60+
61+
62+
[email protected] "//! As are ModuleDoc ..."
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
[email protected] "\"Inner attributes are ..."
81+
82+
83+
84+
[email protected] "//! As are ModuleDoc ..."
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+
120+
[email protected] "\"Outer attributes are ..."
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
[email protected] "// https://github.com ..."
131+
132+
133+
134+
135+
136+
137+
138+
139+
[email protected] "Whatever"
140+
141+
142+
143+
144+
145+
146+
147+
148+
[email protected] "salsa_event"
149+
150+
151+
152+
153+
154+
155+
156+
157+
158+
159+
160+
[email protected] "event_fn"
161+
162+
163+
164+
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+
175+
176+
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+
187+
188+
189+
190+
191+
192+
193+
194+
195+
196+
197+
198+
199+
200+
201+
202+
203+
204+
205+
206+
207+
208+
209+
[email protected] "unused_variables"
210+
211+
212+
213+
[email protected] "// this is `inner_at ..."
214+
215+
216+
217+
218+

crates/syntax/test_data/parser/ok/0045_block_inner_attrs.rs renamed to crates/syntax/test_data/parser/ok/0045_block_attrs.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fn block() {
1+
fn inner() {
22
#![doc("Inner attributes allowed here")]
33
//! As are ModuleDoc style comments
44
{
@@ -12,6 +12,10 @@ fn block() {
1212
}
1313
}
1414

15+
fn outer() {
16+
let _ = #[doc("Outer attributes are always allowed")] {};
17+
}
18+
1519
// https://github.com/rust-analyzer/rust-analyzer/issues/689
1620
impl Whatever {
1721
fn salsa_event(&self, event_fn: impl Fn() -> Event<Self>) {

0 commit comments

Comments
 (0)