|
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::FileName; |
5 | 7 |
|
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; |
| 8 | +#[derive(Default)] |
| 9 | +struct AttrGroup { |
| 10 | + inner_doc: FxHashSet<FileName>, |
| 11 | + outer_doc: FxHashSet<FileName>, |
| 12 | + inner_normal: FxHashSet<FileName>, |
| 13 | + outer_normal: FxHashSet<FileName>, |
| 14 | +} |
| 15 | + |
| 16 | +pub(super) fn check(cx: &LateContext<'_>, attrs: &[Attribute]) { |
| 17 | + let mut attr_group = AttrGroup::default(); |
11 | 18 |
|
12 |
| - for attr in &item.attrs { |
| 19 | + for attr in attrs { |
13 | 20 | if attr.span.from_expansion() {
|
14 | 21 | continue;
|
15 | 22 | }
|
| 23 | + // Keep tracking of the filename of the attribute to prevent linting across files, |
| 24 | + // such as on outlined mod declarations. |
| 25 | + let filename = cx.sess().source_map().span_to_filename(attr.span); |
16 | 26 | 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, |
21 |
| - } |
| 27 | + (AttrStyle::Inner, AttrKind::DocComment(..)) => attr_group.inner_doc.insert(filename), |
| 28 | + (AttrStyle::Outer, AttrKind::DocComment(..)) => attr_group.outer_doc.insert(filename), |
| 29 | + (AttrStyle::Inner, AttrKind::Normal(..)) => attr_group.inner_normal.insert(filename), |
| 30 | + (AttrStyle::Outer, AttrKind::Normal(..)) => attr_group.outer_normal.insert(filename), |
| 31 | + }; |
22 | 32 | }
|
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 |
| - ); |
| 33 | + |
| 34 | + if !should_lint(&attr_group) { |
| 35 | + return; |
34 | 36 | }
|
| 37 | + let mut attrs_iter = attrs.iter().filter(|attr| !attr.span.from_expansion()); |
| 38 | + let span = if let (Some(first), Some(last)) = (attrs_iter.next(), attrs_iter.last()) { |
| 39 | + first.span.with_hi(last.span.hi()) |
| 40 | + } else { |
| 41 | + return; |
| 42 | + }; |
| 43 | + span_lint( |
| 44 | + cx, |
| 45 | + MIXED_ATTRIBUTES_STYLE, |
| 46 | + span, |
| 47 | + "item has both inner and outer attributes", |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +/// Lint only when the attributes: |
| 52 | +/// |
| 53 | +/// - Have the same kind. |
| 54 | +/// - In the same file. |
| 55 | +fn should_lint(attrs: &AttrGroup) -> bool { |
| 56 | + let has_mixed_doc_attrs = !attrs.inner_doc.is_empty() && !attrs.outer_doc.is_empty(); |
| 57 | + let mixed_doc_attrs_in_same_file = attrs.inner_doc.intersection(&attrs.outer_doc).next().is_some(); |
| 58 | + let has_mixed_normal_attrs = !attrs.inner_normal.is_empty() && !attrs.outer_normal.is_empty(); |
| 59 | + let mixed_normal_attrs_in_same_file = attrs.inner_normal.intersection(&attrs.outer_normal).next().is_some(); |
| 60 | + |
| 61 | + (has_mixed_doc_attrs && mixed_doc_attrs_in_same_file) || (has_mixed_normal_attrs && mixed_normal_attrs_in_same_file) |
35 | 62 | }
|
0 commit comments