Skip to content

Commit

Permalink
don't lint [mixed_attributes_style] when mixing docs and other attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed Mar 14, 2024
1 parent 14345ee commit 8147990
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
36 changes: 20 additions & 16 deletions clippy_lints/src/attrs/mixed_attributes_style.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
use super::MIXED_ATTRIBUTES_STYLE;
use clippy_utils::diagnostics::span_lint;
use rustc_ast::AttrStyle;
use rustc_ast::{AttrKind, AttrStyle};
use rustc_lint::EarlyContext;

pub(super) fn check(cx: &EarlyContext<'_>, item: &rustc_ast::Item) {
let mut has_outer = false;
let mut has_inner = false;
let mut has_outer_normal = false;
let mut has_inner_normal = false;
let mut has_outer_doc = false;
let mut has_inner_doc = false;

for attr in &item.attrs {
if attr.span.from_expansion() {
continue;
}
match attr.style {
AttrStyle::Inner => has_inner = true,
AttrStyle::Outer => has_outer = true,
match (&attr.style, &attr.kind) {
(AttrStyle::Inner, AttrKind::Normal(_)) => has_inner_normal = true,
(AttrStyle::Inner, AttrKind::DocComment(..)) => has_inner_doc = true,
(AttrStyle::Outer, AttrKind::Normal(_)) => has_outer_normal = true,
(AttrStyle::Outer, AttrKind::DocComment(..)) => has_outer_doc = true,
}
}
if !has_outer || !has_inner {
return;
// Inner/Outer styled doc does not conflict with normal attrs.
if (has_inner_doc && has_outer_doc) || (has_outer_normal && has_inner_normal) {
let mut attrs_iter = item.attrs.iter().filter(|attr| !attr.span.from_expansion());
let span = attrs_iter.next().unwrap().span;
span_lint(
cx,
MIXED_ATTRIBUTES_STYLE,
span.with_hi(attrs_iter.last().unwrap().span.hi()),
"item has both inner and outer attributes",
);
}
let mut attrs_iter = item.attrs.iter().filter(|attr| !attr.span.from_expansion());
let span = attrs_iter.next().unwrap().span;
span_lint(
cx,
MIXED_ATTRIBUTES_STYLE,
span.with_hi(attrs_iter.last().unwrap().span.hi()),
"item has both inner and outer attributes",
);
}
26 changes: 1 addition & 25 deletions tests/ui/mixed_attributes_style.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,5 @@ LL | | mod bar {
LL | | #![allow(unused)]
| |_____________________^

error: item has both inner and outer attributes
--> tests/ui/mixed_attributes_style.rs:43:1
|
LL | / #[cfg(test)]
LL | | mod tests {
LL | | //! Module doc, don't lint
| |______________________________^

error: item has both inner and outer attributes
--> tests/ui/mixed_attributes_style.rs:47:1
|
LL | / #[allow(unused)]
LL | | mod baz {
LL | | //! Module doc, don't lint
| |______________________________^

error: item has both inner and outer attributes
--> tests/ui/mixed_attributes_style.rs:52:1
|
LL | / /// Module doc, don't lint
LL | | mod quz {
LL | | #![allow(unused)]
| |_____________________^

error: aborting due to 6 previous errors
error: aborting due to 3 previous errors

0 comments on commit 8147990

Please sign in to comment.