-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't lint [
mixed_attributes_style
] when mixing docs and other attrs
- Loading branch information
Showing
2 changed files
with
21 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters