Skip to content

Commit 10daf8a

Browse files
authored
Rollup merge of rust-lang#123001 - Alexendoo:check-attributes, r=oli-obk
Rename `{enter,exit}_lint_attrs` to `check_attributes{,_post}` Several places in Clippy want to check all the attributes of a node, we end up using `hir().attrs()` from several different `check_` functions (e.g. [in our doc lints](https://github.com/rust-lang/rust-clippy/blob/95c62ffae9bbce793f68a6f1473e3fc24af19bdd/clippy_lints/src/doc/mod.rs#L396)) but this is error prone, we recently found that doc lints weren't triggering on struct fields for example I went to add a `check_attributes` function but realised `enter_lint_attrs` is already this, the rename is to encourage their use Also removes `LateContextAndPass::visit_attribute` since it's unused - `visit_attribute` for HIR visitors is only called by `hir().walk_attributes()` which lint passes do not use
2 parents 657dd0b + 2cb5347 commit 10daf8a

File tree

8 files changed

+19
-34
lines changed

8 files changed

+19
-34
lines changed

compiler/rustc_lint/src/early.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
7373

7474
self.inlined_check_id(id);
7575
debug!("early context: enter_attrs({:?})", attrs);
76-
lint_callback!(self, enter_lint_attrs, attrs);
76+
lint_callback!(self, check_attributes, attrs);
7777
ensure_sufficient_stack(|| f(self));
7878
debug!("early context: exit_attrs({:?})", attrs);
79-
lint_callback!(self, exit_lint_attrs, attrs);
79+
lint_callback!(self, check_attributes_post, attrs);
8080
self.context.builder.pop(push);
8181
}
8282
}

compiler/rustc_lint/src/late.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//! for all lint attributes.
1616
1717
use crate::{passes::LateLintPassObject, LateContext, LateLintPass, LintStore};
18-
use rustc_ast as ast;
1918
use rustc_data_structures::stack::ensure_sufficient_stack;
2019
use rustc_data_structures::sync::{join, Lrc};
2120
use rustc_hir as hir;
@@ -62,13 +61,13 @@ impl<'tcx, T: LateLintPass<'tcx>> LateContextAndPass<'tcx, T> {
6261
let prev = self.context.last_node_with_lint_attrs;
6362
self.context.last_node_with_lint_attrs = id;
6463
debug!("late context: enter_attrs({:?})", attrs);
65-
lint_callback!(self, enter_lint_attrs, attrs);
64+
lint_callback!(self, check_attributes, attrs);
6665
for attr in attrs {
6766
lint_callback!(self, check_attribute, attr);
6867
}
6968
f(self);
7069
debug!("late context: exit_attrs({:?})", attrs);
71-
lint_callback!(self, exit_lint_attrs, attrs);
70+
lint_callback!(self, check_attributes_post, attrs);
7271
self.context.last_node_with_lint_attrs = prev;
7372
}
7473

@@ -310,10 +309,6 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
310309
lint_callback!(self, check_path, p, id);
311310
hir_visit::walk_path(self, p);
312311
}
313-
314-
fn visit_attribute(&mut self, attr: &'tcx ast::Attribute) {
315-
lint_callback!(self, check_attribute, attr);
316-
}
317312
}
318313

319314
// Combines multiple lint passes into a single pass, at runtime. Each

compiler/rustc_lint/src/passes.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,8 @@ macro_rules! late_lint_methods {
4141
fn check_variant(a: &'tcx rustc_hir::Variant<'tcx>);
4242
fn check_path(a: &rustc_hir::Path<'tcx>, b: rustc_hir::HirId);
4343
fn check_attribute(a: &'tcx rustc_ast::Attribute);
44-
45-
/// Called when entering a syntax node that can have lint attributes such
46-
/// as `#[allow(...)]`. Called with *all* the attributes of that node.
47-
fn enter_lint_attrs(a: &'tcx [rustc_ast::Attribute]);
48-
49-
/// Counterpart to `enter_lint_attrs`.
50-
fn exit_lint_attrs(a: &'tcx [rustc_ast::Attribute]);
44+
fn check_attributes(a: &'tcx [rustc_ast::Attribute]);
45+
fn check_attributes_post(a: &'tcx [rustc_ast::Attribute]);
5146
]);
5247
)
5348
}
@@ -162,16 +157,11 @@ macro_rules! early_lint_methods {
162157
fn check_impl_item(a: &rustc_ast::AssocItem);
163158
fn check_variant(a: &rustc_ast::Variant);
164159
fn check_attribute(a: &rustc_ast::Attribute);
160+
fn check_attributes(a: &[rustc_ast::Attribute]);
161+
fn check_attributes_post(a: &[rustc_ast::Attribute]);
165162
fn check_mac_def(a: &rustc_ast::MacroDef);
166163
fn check_mac(a: &rustc_ast::MacCall);
167164

168-
/// Called when entering a syntax node that can have lint attributes such
169-
/// as `#[allow(...)]`. Called with *all* the attributes of that node.
170-
fn enter_lint_attrs(a: &[rustc_ast::Attribute]);
171-
172-
/// Counterpart to `enter_lint_attrs`.
173-
fn exit_lint_attrs(a: &[rustc_ast::Attribute]);
174-
175165
fn enter_where_predicate(a: &rustc_ast::WherePredicate);
176166
fn exit_where_predicate(a: &rustc_ast::WherePredicate);
177167
]);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ impl Msrv {
143143
None
144144
}
145145

146-
pub fn enter_lint_attrs(&mut self, sess: &Session, attrs: &[Attribute]) {
146+
pub fn check_attributes(&mut self, sess: &Session, attrs: &[Attribute]) {
147147
if let Some(version) = Self::parse_attr(sess, attrs) {
148148
self.stack.push(version);
149149
}
150150
}
151151

152-
pub fn exit_lint_attrs(&mut self, sess: &Session, attrs: &[Attribute]) {
152+
pub fn check_attributes_post(&mut self, sess: &Session, attrs: &[Attribute]) {
153153
if Self::parse_attr(sess, attrs).is_some() {
154154
self.stack.pop();
155155
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ impl<'tcx> LateLintPass<'tcx> for CognitiveComplexity {
158158
}
159159
}
160160

161-
fn enter_lint_attrs(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
161+
fn check_attributes(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
162162
self.limit.push_attrs(cx.sess(), attrs, "cognitive_complexity");
163163
}
164-
fn exit_lint_attrs(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
164+
fn check_attributes_post(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
165165
self.limit.pop_attrs(cx.sess(), attrs, "cognitive_complexity");
166166
}
167167
}

src/tools/clippy/clippy_lints/src/missing_doc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ impl MissingDoc {
162162
impl_lint_pass!(MissingDoc => [MISSING_DOCS_IN_PRIVATE_ITEMS]);
163163

164164
impl<'tcx> LateLintPass<'tcx> for MissingDoc {
165-
fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, attrs: &'tcx [ast::Attribute]) {
165+
fn check_attributes(&mut self, _: &LateContext<'tcx>, attrs: &'tcx [ast::Attribute]) {
166166
let doc_hidden = self.doc_hidden() || is_doc_hidden(attrs);
167167
self.doc_hidden_stack.push(doc_hidden);
168168
}
169169

170-
fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [ast::Attribute]) {
170+
fn check_attributes_post(&mut self, _: &LateContext<'tcx>, _: &'tcx [ast::Attribute]) {
171171
self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
172172
}
173173

src/tools/clippy/clippy_lints/src/utils/internal_lints/msrv_attr_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl LateLintPass<'_> for MsrvAttrImpl {
4242
.filter(|t| matches!(t.unpack(), GenericArgKind::Type(_)))
4343
.any(|t| match_type(cx, t.expect_ty(), &paths::MSRV))
4444
})
45-
&& !items.iter().any(|item| item.ident.name == sym!(enter_lint_attrs))
45+
&& !items.iter().any(|item| item.ident.name == sym!(check_attributes))
4646
{
4747
let context = if is_late_pass { "LateContext" } else { "EarlyContext" };
4848
let lint_pass = if is_late_pass { "LateLintPass" } else { "EarlyLintPass" };

src/tools/clippy/clippy_utils/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ use rustc_middle::hir::nested_filter;
131131
#[macro_export]
132132
macro_rules! extract_msrv_attr {
133133
($context:ident) => {
134-
fn enter_lint_attrs(&mut self, cx: &rustc_lint::$context<'_>, attrs: &[rustc_ast::ast::Attribute]) {
134+
fn check_attributes(&mut self, cx: &rustc_lint::$context<'_>, attrs: &[rustc_ast::ast::Attribute]) {
135135
let sess = rustc_lint::LintContext::sess(cx);
136-
self.msrv.enter_lint_attrs(sess, attrs);
136+
self.msrv.check_attributes(sess, attrs);
137137
}
138138

139-
fn exit_lint_attrs(&mut self, cx: &rustc_lint::$context<'_>, attrs: &[rustc_ast::ast::Attribute]) {
139+
fn check_attributes_post(&mut self, cx: &rustc_lint::$context<'_>, attrs: &[rustc_ast::ast::Attribute]) {
140140
let sess = rustc_lint::LintContext::sess(cx);
141-
self.msrv.exit_lint_attrs(sess, attrs);
141+
self.msrv.check_attributes_post(sess, attrs);
142142
}
143143
};
144144
}

0 commit comments

Comments
 (0)