-
Notifications
You must be signed in to change notification settings - Fork 1.7k
don't lint [mixed_attributes_style
] when mixing docs and other attrs
#12486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+214
−23
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,85 @@ | ||
use super::MIXED_ATTRIBUTES_STYLE; | ||
use clippy_utils::diagnostics::span_lint; | ||
use rustc_ast::AttrStyle; | ||
use rustc_lint::EarlyContext; | ||
use rustc_ast::{AttrKind, AttrStyle, Attribute}; | ||
use rustc_data_structures::fx::FxHashSet; | ||
use rustc_lint::{LateContext, LintContext}; | ||
use rustc_span::source_map::SourceMap; | ||
use rustc_span::{SourceFile, Span, Symbol}; | ||
use std::sync::Arc; | ||
|
||
pub(super) fn check(cx: &EarlyContext<'_>, item: &rustc_ast::Item) { | ||
let mut has_outer = false; | ||
let mut has_inner = false; | ||
#[derive(Hash, PartialEq, Eq)] | ||
enum SimpleAttrKind { | ||
Doc, | ||
/// A normal attribute, with its name symbols. | ||
Normal(Vec<Symbol>), | ||
} | ||
|
||
impl From<&AttrKind> for SimpleAttrKind { | ||
fn from(value: &AttrKind) -> Self { | ||
match value { | ||
AttrKind::Normal(attr) => { | ||
let path_symbols = attr | ||
.item | ||
.path | ||
.segments | ||
.iter() | ||
.map(|seg| seg.ident.name) | ||
.collect::<Vec<_>>(); | ||
Self::Normal(path_symbols) | ||
}, | ||
AttrKind::DocComment(..) => Self::Doc, | ||
} | ||
} | ||
} | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute]) { | ||
let mut inner_attr_kind: FxHashSet<SimpleAttrKind> = FxHashSet::default(); | ||
let mut outer_attr_kind: FxHashSet<SimpleAttrKind> = FxHashSet::default(); | ||
|
||
let source_map = cx.sess().source_map(); | ||
let item_src = source_map.lookup_source_file(item_span.lo()); | ||
|
||
for attr in &item.attrs { | ||
if attr.span.from_expansion() { | ||
for attr in attrs { | ||
if attr.span.from_expansion() || !attr_in_same_src_as_item(source_map, &item_src, attr.span) { | ||
continue; | ||
} | ||
|
||
let kind: SimpleAttrKind = (&attr.kind).into(); | ||
match attr.style { | ||
AttrStyle::Inner => has_inner = true, | ||
AttrStyle::Outer => has_outer = true, | ||
} | ||
AttrStyle::Inner => { | ||
if outer_attr_kind.contains(&kind) { | ||
lint_mixed_attrs(cx, attrs); | ||
return; | ||
} | ||
inner_attr_kind.insert(kind); | ||
}, | ||
AttrStyle::Outer => { | ||
if inner_attr_kind.contains(&kind) { | ||
lint_mixed_attrs(cx, attrs); | ||
return; | ||
} | ||
outer_attr_kind.insert(kind); | ||
}, | ||
}; | ||
} | ||
if !has_outer || !has_inner { | ||
} | ||
|
||
fn lint_mixed_attrs(cx: &LateContext<'_>, attrs: &[Attribute]) { | ||
let mut attrs_iter = attrs.iter().filter(|attr| !attr.span.from_expansion()); | ||
let span = if let (Some(first), Some(last)) = (attrs_iter.next(), attrs_iter.last()) { | ||
first.span.with_hi(last.span.hi()) | ||
} else { | ||
return; | ||
} | ||
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()), | ||
span, | ||
"item has both inner and outer attributes", | ||
); | ||
} | ||
|
||
fn attr_in_same_src_as_item(source_map: &SourceMap, item_src: &Arc<SourceFile>, attr_span: Span) -> bool { | ||
let attr_src = source_map.lookup_source_file(attr_span.lo()); | ||
Arc::ptr_eq(item_src, &attr_src) | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//! Module level doc | ||
#![allow(dead_code)] | ||
|
||
#[allow(unused)] | ||
//~^ ERROR: item has both inner and outer attributes | ||
mod foo { | ||
#![allow(dead_code)] | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// issue 12436 | ||
#![allow(clippy::mixed_attributes_style)] | ||
|
||
#[path = "auxiliary/submodule.rs"] | ||
mod submodule; | ||
|
||
fn main() {} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#[path = "auxiliary/submodule.rs"] // don't lint. | ||
/// This doc comment should not lint, it could be used to add context to the original module doc | ||
mod submodule; |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: item has both inner and outer attributes | ||
--> tests/ui/mixed_attributes_style/auxiliary/submodule.rs:5:1 | ||
| | ||
LL | / #[allow(unused)] | ||
LL | | | ||
LL | | mod foo { | ||
LL | | #![allow(dead_code)] | ||
| |________________________^ | ||
| | ||
= note: `-D clippy::mixed-attributes-style` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::mixed_attributes_style)]` | ||
|
||
error: aborting due to 1 previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.