|
1 | 1 | use super::MIXED_ATTRIBUTES_STYLE; |
2 | 2 | use clippy_utils::diagnostics::span_lint; |
3 | | -use rustc_ast::AttrStyle; |
| 3 | +use rustc_ast::{AttrKind, AttrStyle}; |
4 | 4 | use rustc_lint::EarlyContext; |
5 | 5 |
|
6 | 6 | 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; |
9 | 11 |
|
10 | 12 | for attr in &item.attrs { |
11 | 13 | if attr.span.from_expansion() { |
12 | 14 | continue; |
13 | 15 | } |
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, |
17 | 21 | } |
18 | 22 | } |
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 | + ); |
21 | 34 | } |
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 | | - ); |
30 | 35 | } |
0 commit comments