Skip to content

Commit 88e1770

Browse files
committed
don't lint [mixed_attributes_style] when mixing docs and other attrs
1 parent 14345ee commit 88e1770

File tree

2 files changed

+22
-41
lines changed

2 files changed

+22
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
use super::MIXED_ATTRIBUTES_STYLE;
22
use clippy_utils::diagnostics::span_lint;
3-
use rustc_ast::AttrStyle;
3+
use rustc_ast::{AttrKind, AttrStyle};
44
use rustc_lint::EarlyContext;
55

66
pub(super) fn check(cx: &EarlyContext<'_>, item: &rustc_ast::Item) {
7-
let mut has_outer = false;
8-
let mut has_inner = false;
7+
let mut has_outer_normal = false;
8+
let mut has_inner_normal = false;
9+
let mut has_outer_doc = false;
10+
let mut has_inner_doc = false;
911

1012
for attr in &item.attrs {
1113
if attr.span.from_expansion() {
1214
continue;
1315
}
14-
match attr.style {
15-
AttrStyle::Inner => has_inner = true,
16-
AttrStyle::Outer => has_outer = true,
16+
match (&attr.style, &attr.kind) {
17+
(AttrStyle::Inner, AttrKind::Normal(_)) => has_inner_normal = true,
18+
(AttrStyle::Inner, AttrKind::DocComment(..)) => has_inner_doc = true,
19+
(AttrStyle::Outer, AttrKind::Normal(_)) => has_outer_normal = true,
20+
(AttrStyle::Outer, AttrKind::DocComment(..)) => has_outer_doc = true,
1721
}
1822
}
19-
if !has_outer || !has_inner {
20-
return;
23+
// Separating doc and normal attributes because mixing inner/outer docs
24+
// with other outer/inner attributes doesn't really affecting readability.
25+
if (has_inner_doc && has_outer_doc) || (has_outer_normal && has_inner_normal) {
26+
let mut attrs_iter = item.attrs.iter().filter(|attr| !attr.span.from_expansion());
27+
let span = attrs_iter.next().unwrap().span;
28+
span_lint(
29+
cx,
30+
MIXED_ATTRIBUTES_STYLE,
31+
span.with_hi(attrs_iter.last().unwrap().span.hi()),
32+
"item has both inner and outer attributes",
33+
);
2134
}
22-
let mut attrs_iter = item.attrs.iter().filter(|attr| !attr.span.from_expansion());
23-
let span = attrs_iter.next().unwrap().span;
24-
span_lint(
25-
cx,
26-
MIXED_ATTRIBUTES_STYLE,
27-
span.with_hi(attrs_iter.last().unwrap().span.hi()),
28-
"item has both inner and outer attributes",
29-
);
3035
}

tests/ui/mixed_attributes_style.stderr

+1-25
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,5 @@ LL | | mod bar {
2626
LL | | #![allow(unused)]
2727
| |_____________________^
2828

29-
error: item has both inner and outer attributes
30-
--> tests/ui/mixed_attributes_style.rs:43:1
31-
|
32-
LL | / #[cfg(test)]
33-
LL | | mod tests {
34-
LL | | //! Module doc, don't lint
35-
| |______________________________^
36-
37-
error: item has both inner and outer attributes
38-
--> tests/ui/mixed_attributes_style.rs:47:1
39-
|
40-
LL | / #[allow(unused)]
41-
LL | | mod baz {
42-
LL | | //! Module doc, don't lint
43-
| |______________________________^
44-
45-
error: item has both inner and outer attributes
46-
--> tests/ui/mixed_attributes_style.rs:52:1
47-
|
48-
LL | / /// Module doc, don't lint
49-
LL | | mod quz {
50-
LL | | #![allow(unused)]
51-
| |_____________________^
52-
53-
error: aborting due to 6 previous errors
29+
error: aborting due to 3 previous errors
5430

0 commit comments

Comments
 (0)