From 814799065cfc38323132d03062e4c959f1a82feb Mon Sep 17 00:00:00 2001 From: J-ZhengLi Date: Thu, 14 Mar 2024 17:09:32 +0800 Subject: [PATCH] don't lint [`mixed_attributes_style`] when mixing docs and other attrs --- .../src/attrs/mixed_attributes_style.rs | 36 ++++++++++--------- tests/ui/mixed_attributes_style.stderr | 26 +------------- 2 files changed, 21 insertions(+), 41 deletions(-) diff --git a/clippy_lints/src/attrs/mixed_attributes_style.rs b/clippy_lints/src/attrs/mixed_attributes_style.rs index c2e21cfd3300..fa54060ad8fc 100644 --- a/clippy_lints/src/attrs/mixed_attributes_style.rs +++ b/clippy_lints/src/attrs/mixed_attributes_style.rs @@ -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", - ); } diff --git a/tests/ui/mixed_attributes_style.stderr b/tests/ui/mixed_attributes_style.stderr index 02b19cc98468..ed798073cb7c 100644 --- a/tests/ui/mixed_attributes_style.stderr +++ b/tests/ui/mixed_attributes_style.stderr @@ -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