|
1 | 1 | use super::MIXED_ATTRIBUTES_STYLE;
|
2 | 2 | use clippy_utils::diagnostics::span_lint;
|
3 |
| -use rustc_ast::{AttrKind, AttrStyle}; |
4 |
| -use rustc_lint::EarlyContext; |
| 3 | +use rustc_ast::{AttrKind, AttrStyle, Attribute}; |
| 4 | +use rustc_data_structures::fx::FxHashSet; |
| 5 | +use rustc_lint::{LateContext, LintContext}; |
| 6 | +use rustc_span::Span; |
| 7 | +use std::sync::Arc; |
5 | 8 |
|
6 |
| -pub(super) fn check(cx: &EarlyContext<'_>, item: &rustc_ast::Item) { |
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 | +#[derive(Hash, PartialEq, Eq)] |
| 10 | +enum SimpleAttrKind { |
| 11 | + Doc, |
| 12 | + Normal, |
| 13 | +} |
11 | 14 |
|
12 |
| - for attr in &item.attrs { |
13 |
| - if attr.span.from_expansion() { |
14 |
| - continue; |
15 |
| - } |
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, |
| 15 | +impl From<&AttrKind> for SimpleAttrKind { |
| 16 | + fn from(value: &AttrKind) -> Self { |
| 17 | + match value { |
| 18 | + AttrKind::Normal(_) => Self::Normal, |
| 19 | + AttrKind::DocComment(..) => Self::Doc, |
21 | 20 | }
|
22 | 21 | }
|
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 |
| - ); |
| 22 | +} |
| 23 | + |
| 24 | +pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute]) { |
| 25 | + let mut inner_attr_kind: FxHashSet<SimpleAttrKind> = FxHashSet::default(); |
| 26 | + let mut outer_attr_kind: FxHashSet<SimpleAttrKind> = FxHashSet::default(); |
| 27 | + |
| 28 | + for attr in attrs { |
| 29 | + if attr.span.from_expansion() || !attr_in_same_src_as_item(cx, attr.span, item_span) { |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + let kind: SimpleAttrKind = (&attr.kind).into(); |
| 34 | + match attr.style { |
| 35 | + AttrStyle::Inner => { |
| 36 | + if outer_attr_kind.contains(&kind) { |
| 37 | + lint_mixed_attrs(cx, attrs); |
| 38 | + return; |
| 39 | + } |
| 40 | + inner_attr_kind.insert(kind); |
| 41 | + }, |
| 42 | + AttrStyle::Outer => { |
| 43 | + if inner_attr_kind.contains(&kind) { |
| 44 | + lint_mixed_attrs(cx, attrs); |
| 45 | + return; |
| 46 | + } |
| 47 | + outer_attr_kind.insert(kind); |
| 48 | + }, |
| 49 | + }; |
34 | 50 | }
|
35 | 51 | }
|
| 52 | + |
| 53 | +fn lint_mixed_attrs(cx: &LateContext<'_>, attrs: &[Attribute]) { |
| 54 | + let mut attrs_iter = attrs.iter().filter(|attr| !attr.span.from_expansion()); |
| 55 | + let span = if let (Some(first), Some(last)) = (attrs_iter.next(), attrs_iter.last()) { |
| 56 | + first.span.with_hi(last.span.hi()) |
| 57 | + } else { |
| 58 | + return; |
| 59 | + }; |
| 60 | + span_lint( |
| 61 | + cx, |
| 62 | + MIXED_ATTRIBUTES_STYLE, |
| 63 | + span, |
| 64 | + "item has both inner and outer attributes", |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +fn attr_in_same_src_as_item(cx: &LateContext<'_>, attr_span: Span, item_span: Span) -> bool { |
| 69 | + let source_map = cx.sess().source_map(); |
| 70 | + let item_src = source_map.lookup_source_file(item_span.lo()); |
| 71 | + let attr_src = source_map.lookup_source_file(attr_span.lo()); |
| 72 | + Arc::ptr_eq(&attr_src, &item_src) |
| 73 | +} |
0 commit comments