Skip to content

Commit 95646b8

Browse files
committed
move [mixed_attributes_style] to LateLintPass;
fix issue #12436;
1 parent c6f794a commit 95646b8

File tree

5 files changed

+63
-27
lines changed

5 files changed

+63
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,62 @@
11
use super::MIXED_ATTRIBUTES_STYLE;
22
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;
57

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();
1118

12-
for attr in &item.attrs {
19+
for attr in attrs {
1320
if attr.span.from_expansion() {
1421
continue;
1522
}
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);
1626
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+
};
2232
}
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;
3436
}
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)
3562
}

clippy_lints/src/attrs/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ declare_lint_pass!(Attributes => [
523523
USELESS_ATTRIBUTE,
524524
BLANKET_CLIPPY_RESTRICTION_LINTS,
525525
SHOULD_PANIC_WITHOUT_EXPECT,
526+
MIXED_ATTRIBUTES_STYLE,
526527
]);
527528

528529
impl<'tcx> LateLintPass<'tcx> for Attributes {
@@ -566,6 +567,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
566567
ItemKind::ExternCrate(..) | ItemKind::Use(..) => useless_attribute::check(cx, item, attrs),
567568
_ => {},
568569
}
570+
mixed_attributes_style::check(cx, attrs);
569571
}
570572

571573
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
@@ -594,7 +596,6 @@ impl_lint_pass!(EarlyAttributes => [
594596
MAYBE_MISUSED_CFG,
595597
DEPRECATED_CLIPPY_CFG_ATTR,
596598
UNNECESSARY_CLIPPY_CFG,
597-
MIXED_ATTRIBUTES_STYLE,
598599
DUPLICATED_ATTRIBUTES,
599600
]);
600601

@@ -605,7 +606,6 @@ impl EarlyLintPass for EarlyAttributes {
605606

606607
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &rustc_ast::Item) {
607608
empty_line_after::check(cx, item);
608-
mixed_attributes_style::check(cx, item);
609609
duplicated_attributes::check(cx, &item.attrs);
610610
}
611611

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
//! Module level doc
2+
3+
#![allow(dead_code)]
4+
15
#[allow(unused)]
6+
//~^ ERROR: item has both inner and outer attributes
27
mod foo {
38
#![allow(dead_code)]
49
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#[path = "auxiliary/submodule.rs"] // don't lint.
2+
/// This doc comment should not lint, it could be used to add context to the original module doc
3+
mod submodule;

tests/ui/mixed_attributes_style/global_allow.stderr tests/ui/mixed_attributes_style/mod_declaration.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
error: item has both inner and outer attributes
2-
--> tests/ui/mixed_attributes_style/auxiliary/submodule.rs:1:1
2+
--> tests/ui/mixed_attributes_style/auxiliary/submodule.rs:5:1
33
|
44
LL | / #[allow(unused)]
5+
LL | |
56
LL | | mod foo {
67
LL | | #![allow(dead_code)]
78
| |________________________^

0 commit comments

Comments
 (0)