-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #124369 - flip1995:clippy-backport, r=Mark-Simulacrum
[beta] Clippy backport r? `@Mark-Simulacrum` Backports: - rust-lang/rust-clippy#12486 - rust-lang/rust-clippy#12572 - rust-lang/rust-clippy#12508 - rust-lang/rust-clippy#12617 The first one is a bit bigger as usual for a backport. But it fixes a major issue with this lint that we overlooked. So I think this is worth it. After that was merged into nightly, there were no new issues opened about this lint, so IMO this is safe to backport to `beta` and put into stable.
- Loading branch information
Showing
15 changed files
with
367 additions
and
114 deletions.
There are no files selected for viewing
85 changes: 70 additions & 15 deletions
85
src/tools/clippy/clippy_lints/src/attrs/mixed_attributes_style.rs
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,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_data_structures::sync::Lrc; | ||
use rustc_lint::{LateContext, LintContext}; | ||
use rustc_span::source_map::SourceMap; | ||
use rustc_span::{SourceFile, Span, Symbol}; | ||
|
||
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: &Lrc<SourceFile>, attr_span: Span) -> bool { | ||
let attr_src = source_map.lookup_source_file(attr_span.lo()); | ||
Lrc::ptr_eq(item_src, &attr_src) | ||
} |
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
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
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
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
Oops, something went wrong.