Skip to content

Commit c4425ed

Browse files
change clippy to use hir::Attribute
Co-authored-by: xFrednet <[email protected]>
1 parent 04e6a85 commit c4425ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+208
-142
lines changed

src/librustdoc/clean/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
//! This IR is consumed by both the HTML and the JSON backend.
2121
//!
2222
//! [^1]: Intermediate representation.
23-
2423
mod auto_trait;
2524
mod blanket_impl;
2625
pub(crate) mod cfg;

src/tools/clippy/clippy_config/src/msrvs.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::Attribute;
1+
use rustc_ast::attr::AttributeExt;
22
use rustc_attr::parse_version;
33
use rustc_session::{RustcVersion, Session};
44
use rustc_span::{Symbol, sym};
@@ -124,15 +124,15 @@ impl Msrv {
124124
self.current().map_or(true, |msrv| msrv >= required)
125125
}
126126

127-
fn parse_attr(sess: &Session, attrs: &[Attribute]) -> Option<RustcVersion> {
127+
fn parse_attr(sess: &Session, attrs: &[impl AttributeExt]) -> Option<RustcVersion> {
128128
let sym_msrv = Symbol::intern("msrv");
129129
let mut msrv_attrs = attrs.iter().filter(|attr| attr.path_matches(&[sym::clippy, sym_msrv]));
130130

131131
if let Some(msrv_attr) = msrv_attrs.next() {
132132
if let Some(duplicate) = msrv_attrs.last() {
133133
sess.dcx()
134-
.struct_span_err(duplicate.span, "`clippy::msrv` is defined multiple times")
135-
.with_span_note(msrv_attr.span, "first definition found here")
134+
.struct_span_err(duplicate.span(), "`clippy::msrv` is defined multiple times")
135+
.with_span_note(msrv_attr.span(), "first definition found here")
136136
.emit();
137137
}
138138

@@ -142,22 +142,22 @@ impl Msrv {
142142
}
143143

144144
sess.dcx()
145-
.span_err(msrv_attr.span, format!("`{msrv}` is not a valid Rust version"));
145+
.span_err(msrv_attr.span(), format!("`{msrv}` is not a valid Rust version"));
146146
} else {
147-
sess.dcx().span_err(msrv_attr.span, "bad clippy attribute");
147+
sess.dcx().span_err(msrv_attr.span(), "bad clippy attribute");
148148
}
149149
}
150150

151151
None
152152
}
153153

154-
pub fn check_attributes(&mut self, sess: &Session, attrs: &[Attribute]) {
154+
pub fn check_attributes(&mut self, sess: &Session, attrs: &[impl AttributeExt]) {
155155
if let Some(version) = Self::parse_attr(sess, attrs) {
156156
self.stack.push(version);
157157
}
158158
}
159159

160-
pub fn check_attributes_post(&mut self, sess: &Session, attrs: &[Attribute]) {
160+
pub fn check_attributes_post(&mut self, sess: &Session, attrs: &[impl AttributeExt]) {
161161
if Self::parse_attr(sess, attrs).is_some() {
162162
self.stack.pop();
163163
}

src/tools/clippy/clippy_lints/src/attrs/allow_attributes.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use super::ALLOW_ATTRIBUTES;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::is_from_proc_macro;
4-
use rustc_ast::{AttrStyle, Attribute};
4+
use rustc_ast::AttrStyle;
5+
use rustc_ast::attr::AttributeExt;
56
use rustc_errors::Applicability;
7+
use rustc_hir::Attribute;
68
use rustc_lint::{LateContext, LintContext};
79
use rustc_middle::lint::in_external_macro;
810

src/tools/clippy/clippy_lints/src/attrs/allow_attributes_without_reason.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use super::{ALLOW_ATTRIBUTES_WITHOUT_REASON, Attribute};
1+
use super::ALLOW_ATTRIBUTES_WITHOUT_REASON;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::is_from_proc_macro;
44
use rustc_ast::{MetaItemInner, MetaItemKind};
5+
use rustc_hir::Attribute;
56
use rustc_lint::{LateContext, LintContext};
67
use rustc_middle::lint::in_external_macro;
78
use rustc_span::sym;

src/tools/clippy/clippy_lints/src/attrs/deprecated_cfg_attr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use super::{Attribute, DEPRECATED_CFG_ATTR, DEPRECATED_CLIPPY_CFG_ATTR, unnecessary_clippy_cfg};
1+
use super::{DEPRECATED_CFG_ATTR, DEPRECATED_CLIPPY_CFG_ATTR, unnecessary_clippy_cfg};
22
use clippy_config::msrvs::{self, Msrv};
33
use clippy_utils::diagnostics::span_lint_and_sugg;
4-
use rustc_ast::AttrStyle;
4+
use rustc_ast::attr::AttributeExt;
5+
use rustc_ast::{AttrStyle, Attribute};
56
use rustc_errors::Applicability;
67
use rustc_lint::EarlyContext;
78
use rustc_span::sym;

src/tools/clippy/clippy_lints/src/attrs/duplicated_attributes.rs

+63-12
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,83 @@
11
use super::DUPLICATED_ATTRIBUTES;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use rustc_ast::{Attribute, MetaItem};
3+
use rustc_ast::{MetaItem, MetaItemInner};
44
use rustc_data_structures::fx::FxHashMap;
5+
use rustc_hir::Attribute;
56
use rustc_lint::LateContext;
6-
use rustc_span::{Span, sym};
7+
use rustc_span::symbol::Ident;
8+
use rustc_span::{Span, Symbol, sym};
79
use std::collections::hash_map::Entry;
10+
use thin_vec::ThinVec;
811

912
fn emit_if_duplicated(
1013
cx: &LateContext<'_>,
11-
attr: &MetaItem,
14+
span: Span,
1215
attr_paths: &mut FxHashMap<String, Span>,
1316
complete_path: String,
1417
) {
1518
match attr_paths.entry(complete_path) {
1619
Entry::Vacant(v) => {
17-
v.insert(attr.span);
20+
v.insert(span);
1821
},
1922
Entry::Occupied(o) => {
20-
span_lint_and_then(cx, DUPLICATED_ATTRIBUTES, attr.span, "duplicated attribute", |diag| {
23+
span_lint_and_then(cx, DUPLICATED_ATTRIBUTES, span, "duplicated attribute", |diag| {
2124
diag.span_note(*o.get(), "first defined here");
22-
diag.span_help(attr.span, "remove this attribute");
25+
diag.span_help(span, "remove this attribute");
2326
});
2427
},
2528
}
2629
}
2730

31+
trait AttrOrMetaItem {
32+
fn ident(&self) -> Option<Ident>;
33+
fn span(&self) -> Span;
34+
fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>>;
35+
fn value_str(&self) -> Option<Symbol>;
36+
}
37+
38+
impl AttrOrMetaItem for Attribute {
39+
fn ident(&self) -> Option<Ident> {
40+
rustc_ast::attr::AttributeExt::ident(self)
41+
}
42+
43+
fn span(&self) -> Span {
44+
rustc_ast::attr::AttributeExt::span(self)
45+
}
46+
47+
fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
48+
rustc_ast::attr::AttributeExt::meta_item_list(self)
49+
}
50+
51+
fn value_str(&self) -> Option<Symbol> {
52+
rustc_ast::attr::AttributeExt::value_str(self)
53+
}
54+
}
55+
56+
impl AttrOrMetaItem for MetaItem {
57+
fn ident(&self) -> Option<Ident> {
58+
MetaItem::ident(self)
59+
}
60+
61+
fn span(&self) -> Span {
62+
self.span
63+
}
64+
65+
fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
66+
MetaItem::meta_item_list(self).map(|i| i.iter().cloned().collect())
67+
}
68+
69+
fn value_str(&self) -> Option<Symbol> {
70+
MetaItem::value_str(self)
71+
}
72+
}
73+
2874
fn check_duplicated_attr(
2975
cx: &LateContext<'_>,
30-
attr: &MetaItem,
76+
attr: &impl AttrOrMetaItem,
3177
attr_paths: &mut FxHashMap<String, Span>,
3278
parent: &mut Vec<String>,
3379
) {
34-
if attr.span.from_expansion() {
80+
if attr.span().from_expansion() {
3581
return;
3682
}
3783
let Some(ident) = attr.ident() else { return };
@@ -51,7 +97,12 @@ fn check_duplicated_attr(
5197
return;
5298
}
5399
if let Some(value) = attr.value_str() {
54-
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}={value}", parent.join(":")));
100+
emit_if_duplicated(
101+
cx,
102+
attr.span(),
103+
attr_paths,
104+
format!("{}:{name}={value}", parent.join(":")),
105+
);
55106
} else if let Some(sub_attrs) = attr.meta_item_list() {
56107
parent.push(name.as_str().to_string());
57108
for sub_attr in sub_attrs {
@@ -61,16 +112,16 @@ fn check_duplicated_attr(
61112
}
62113
parent.pop();
63114
} else {
64-
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}", parent.join(":")));
115+
emit_if_duplicated(cx, attr.span(), attr_paths, format!("{}:{name}", parent.join(":")));
65116
}
66117
}
67118

68119
pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
69120
let mut attr_paths = FxHashMap::default();
70121

71122
for attr in attrs {
72-
if let Some(meta) = attr.meta() {
73-
check_duplicated_attr(cx, &meta, &mut attr_paths, &mut Vec::new());
123+
if !rustc_ast::attr::AttributeExt::is_doc_comment(attr) {
124+
check_duplicated_attr(cx, attr, &mut attr_paths, &mut Vec::new());
74125
}
75126
}
76127
}

src/tools/clippy/clippy_lints/src/attrs/inline_always.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use super::INLINE_ALWAYS;
22
use super::utils::is_word;
33
use clippy_utils::diagnostics::span_lint;
4-
use rustc_ast::Attribute;
4+
use rustc_ast::attr::AttributeExt;
5+
use rustc_hir::Attribute;
56
use rustc_lint::LateContext;
67
use rustc_span::symbol::Symbol;
78
use rustc_span::{Span, sym};

src/tools/clippy/clippy_lints/src/attrs/mixed_attributes_style.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use super::MIXED_ATTRIBUTES_STYLE;
22
use clippy_utils::diagnostics::span_lint;
3-
use rustc_ast::{AttrKind, AttrStyle, Attribute};
3+
use rustc_ast::AttrStyle;
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_data_structures::sync::Lrc;
6+
use rustc_hir::{AttrKind, Attribute};
67
use rustc_lint::{LateContext, LintContext};
78
use rustc_span::source_map::SourceMap;
89
use rustc_span::{SourceFile, Span, Symbol};
@@ -18,13 +19,7 @@ impl From<&AttrKind> for SimpleAttrKind {
1819
fn from(value: &AttrKind) -> Self {
1920
match value {
2021
AttrKind::Normal(attr) => {
21-
let path_symbols = attr
22-
.item
23-
.path
24-
.segments
25-
.iter()
26-
.map(|seg| seg.ident.name)
27-
.collect::<Vec<_>>();
22+
let path_symbols = attr.path.segments.iter().map(|seg| seg.name).collect::<Vec<_>>();
2823
Self::Normal(path_symbols)
2924
},
3025
AttrKind::DocComment(..) => Self::Doc,

src/tools/clippy/clippy_lints/src/attrs/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ mod utils;
1414

1515
use clippy_config::Conf;
1616
use clippy_config::msrvs::{self, Msrv};
17-
use rustc_ast::{Attribute, MetaItemInner, MetaItemKind};
18-
use rustc_hir::{ImplItem, Item, ItemKind, TraitItem};
17+
use rustc_ast::attr::AttributeExt;
18+
use rustc_ast::{self as ast, MetaItemInner, MetaItemKind};
19+
use rustc_hir::{self as hir, ImplItem, Item, ItemKind, TraitItem};
1920
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
2021
use rustc_session::impl_lint_pass;
2122
use rustc_span::sym;
@@ -439,7 +440,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
439440
duplicated_attributes::check(cx, cx.tcx.hir().krate_attrs());
440441
}
441442

442-
fn check_attribute(&mut self, cx: &LateContext<'tcx>, attr: &'tcx Attribute) {
443+
fn check_attribute(&mut self, cx: &LateContext<'tcx>, attr: &'tcx hir::Attribute) {
443444
if let Some(items) = &attr.meta_item_list() {
444445
if let Some(ident) = attr.ident() {
445446
if is_lint_level(ident.name, attr.id) {
@@ -518,7 +519,7 @@ impl_lint_pass!(EarlyAttributes => [
518519
]);
519520

520521
impl EarlyLintPass for EarlyAttributes {
521-
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
522+
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
522523
deprecated_cfg_attr::check(cx, attr, &self.msrv);
523524
deprecated_cfg_attr::check_clippy(cx, attr);
524525
non_minimal_cfg::check(cx, attr);

src/tools/clippy/clippy_lints/src/attrs/non_minimal_cfg.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use super::{Attribute, NON_MINIMAL_CFG};
1+
use super::NON_MINIMAL_CFG;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::source::SpanRangeExt;
4-
use rustc_ast::{MetaItemInner, MetaItemKind};
4+
use rustc_ast::attr::AttributeExt;
5+
use rustc_ast::{Attribute, MetaItemInner, MetaItemKind};
56
use rustc_errors::Applicability;
67
use rustc_lint::EarlyContext;
78
use rustc_span::sym;

src/tools/clippy/clippy_lints/src/attrs/should_panic_without_expect.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
use super::{Attribute, SHOULD_PANIC_WITHOUT_EXPECT};
1+
use super::SHOULD_PANIC_WITHOUT_EXPECT;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use rustc_ast::attr::AttributeExt;
34
use rustc_ast::token::{Token, TokenKind};
45
use rustc_ast::tokenstream::TokenTree;
5-
use rustc_ast::{AttrArgs, AttrArgsEq, AttrKind};
66
use rustc_errors::Applicability;
7+
use rustc_hir::{AttrArgs, AttrKind, Attribute};
78
use rustc_lint::LateContext;
89
use rustc_span::sym;
910

1011
pub(super) fn check(cx: &LateContext<'_>, attr: &Attribute) {
1112
if let AttrKind::Normal(normal_attr) = &attr.kind {
12-
if let AttrArgs::Eq(_, AttrArgsEq::Hir(_)) = &normal_attr.item.args {
13+
if let AttrArgs::Eq { .. } = &normal_attr.args {
1314
// `#[should_panic = ".."]` found, good
1415
return;
1516
}
1617

17-
if let AttrArgs::Delimited(args) = &normal_attr.item.args
18+
if let AttrArgs::Delimited(args) = &normal_attr.args
1819
&& let mut tt_iter = args.tokens.trees()
1920
&& let Some(TokenTree::Token(
2021
Token {
@@ -44,7 +45,7 @@ pub(super) fn check(cx: &LateContext<'_>, attr: &Attribute) {
4445
span_lint_and_sugg(
4546
cx,
4647
SHOULD_PANIC_WITHOUT_EXPECT,
47-
attr.span,
48+
attr.span(),
4849
"#[should_panic] attribute without a reason",
4950
"consider specifying the expected panic",
5051
"#[should_panic(expected = /* panic message */)]".into(),

src/tools/clippy/clippy_lints/src/attrs/unnecessary_clippy_cfg.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::{Attribute, UNNECESSARY_CLIPPY_CFG};
1+
use super::UNNECESSARY_CLIPPY_CFG;
22
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg};
33
use clippy_utils::source::SpanRangeExt;
44
use itertools::Itertools;
5-
use rustc_ast::AttrStyle;
5+
use rustc_ast::{AttrStyle, Attribute};
66
use rustc_errors::Applicability;
77
use rustc_lint::{EarlyContext, Level};
88
use rustc_span::sym;

src/tools/clippy/clippy_lints/src/attrs/useless_attribute.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
use super::USELESS_ATTRIBUTE;
12
use super::utils::{extract_clippy_lint, is_lint_level, is_word};
2-
use super::{Attribute, USELESS_ATTRIBUTE};
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::source::{SpanRangeExt, first_line_of_span};
55
use rustc_ast::MetaItemInner;
6+
use rustc_ast::attr::AttributeExt;
67
use rustc_errors::Applicability;
7-
use rustc_hir::{Item, ItemKind};
8+
use rustc_hir::{Attribute, Item, ItemKind};
89
use rustc_lint::{LateContext, LintContext};
910
use rustc_middle::lint::in_external_macro;
1011
use rustc_span::sym;

src/tools/clippy/clippy_lints/src/cfg_not_test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_ast::MetaItemInner;
3+
use rustc_ast::attr::AttributeExt;
34
use rustc_lint::{EarlyContext, EarlyLintPass};
45
use rustc_session::declare_lint_pass;
56

src/tools/clippy/clippy_lints/src/cognitive_complexity.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use clippy_utils::ty::is_type_diagnostic_item;
55
use clippy_utils::visitors::for_each_expr_without_closures;
66
use clippy_utils::{LimitStack, get_async_fn_body, is_async_fn};
77
use core::ops::ControlFlow;
8-
use rustc_ast::ast::Attribute;
98
use rustc_hir::intravisit::FnKind;
10-
use rustc_hir::{Body, Expr, ExprKind, FnDecl};
9+
use rustc_hir::{Attribute, Body, Expr, ExprKind, FnDecl};
1110
use rustc_lint::{LateContext, LateLintPass, LintContext};
1211
use rustc_session::impl_lint_pass;
1312
use rustc_span::def_id::LocalDefId;

src/tools/clippy/clippy_lints/src/default_union_representation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use rustc_ast::attr::AttributeExt;
23
use rustc_hir::{HirId, Item, ItemKind};
34
use rustc_lint::{LateContext, LateLintPass};
45
use rustc_middle::ty::layout::LayoutOf;

src/tools/clippy/clippy_lints/src/derivable_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_config::msrvs::{self, Msrv};
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::source::indent_of;
55
use clippy_utils::{is_default_equivalent, peel_blocks};
6+
use rustc_ast::attr::AttributeExt;
67
use rustc_errors::Applicability;
78
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
89
use rustc_hir::{

0 commit comments

Comments
 (0)