Skip to content

Commit 3dcb681

Browse files
authored
Unrolled build for rust-lang#115739
Rollup merge of rust-lang#115739 - Alexendoo:lint-pass-check-attribute, r=oli-obk Call `LateLintPass::check_attribute` from `with_lint_attrs` Fixes rust-lang#115571 For regular `register_late_pass` lints also means that `last_node_with_lint_attrs` is correct when in `check_attribute`, I've added a test that previously failed for `clippy::allow_attributes` As far as I can see the only late lint in rustc that uses `check_attribute` is `unstable_features` which is allow by default and deprecated so this is mostly for clippy (or future rustc lints)
2 parents 3ebb562 + ce3b044 commit 3dcb681

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

compiler/rustc_lint/src/late.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc_data_structures::sync::join;
2121
use rustc_hir as hir;
2222
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
2323
use rustc_hir::intravisit as hir_visit;
24-
use rustc_hir::intravisit::Visitor;
2524
use rustc_middle::hir::nested_filter;
2625
use rustc_middle::ty::{self, TyCtxt};
2726
use rustc_session::lint::LintPass;
@@ -61,6 +60,9 @@ impl<'tcx, T: LateLintPass<'tcx>> LateContextAndPass<'tcx, T> {
6160
self.context.last_node_with_lint_attrs = id;
6261
debug!("late context: enter_attrs({:?})", attrs);
6362
lint_callback!(self, enter_lint_attrs, attrs);
63+
for attr in attrs {
64+
lint_callback!(self, check_attribute, attr);
65+
}
6466
f(self);
6567
debug!("late context: exit_attrs({:?})", attrs);
6668
lint_callback!(self, exit_lint_attrs, attrs);
@@ -377,20 +379,18 @@ fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>(
377379

378380
let (module, _span, hir_id) = tcx.hir().get_module(module_def_id);
379381

380-
// There is no module lint that will have the crate itself as an item, so check it here.
381-
if hir_id == hir::CRATE_HIR_ID {
382-
lint_callback!(cx, check_crate,);
383-
}
382+
cx.with_lint_attrs(hir_id, |cx| {
383+
// There is no module lint that will have the crate itself as an item, so check it here.
384+
if hir_id == hir::CRATE_HIR_ID {
385+
lint_callback!(cx, check_crate,);
386+
}
384387

385-
cx.process_mod(module, hir_id);
388+
cx.process_mod(module, hir_id);
386389

387-
// Visit the crate attributes
388-
if hir_id == hir::CRATE_HIR_ID {
389-
for attr in tcx.hir().attrs(hir::CRATE_HIR_ID).iter() {
390-
cx.visit_attribute(attr)
390+
if hir_id == hir::CRATE_HIR_ID {
391+
lint_callback!(cx, check_crate_post,);
391392
}
392-
lint_callback!(cx, check_crate_post,);
393-
}
393+
});
394394
}
395395

396396
fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
@@ -431,7 +431,6 @@ fn late_lint_crate_inner<'tcx, T: LateLintPass<'tcx>>(
431431
// item), warn for it here.
432432
lint_callback!(cx, check_crate,);
433433
tcx.hir().walk_toplevel_module(cx);
434-
tcx.hir().walk_attributes(cx);
435434
lint_callback!(cx, check_crate_post,);
436435
})
437436
}

src/tools/clippy/tests/ui/allow_attributes.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ struct T4;
2222
#[cfg_attr(panic = "unwind", expect(dead_code))]
2323
struct CfgT;
2424

25+
#[allow(clippy::allow_attributes, unused)]
26+
struct Allowed;
27+
28+
#[expect(clippy::allow_attributes)]
29+
#[allow(unused)]
30+
struct Expected;
31+
2532
fn ignore_external() {
2633
external! {
2734
#[allow(clippy::needless_borrow)] // Should not lint

src/tools/clippy/tests/ui/allow_attributes.rs

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ struct T4;
2222
#[cfg_attr(panic = "unwind", allow(dead_code))]
2323
struct CfgT;
2424

25+
#[allow(clippy::allow_attributes, unused)]
26+
struct Allowed;
27+
28+
#[expect(clippy::allow_attributes)]
29+
#[allow(unused)]
30+
struct Expected;
31+
2532
fn ignore_external() {
2633
external! {
2734
#[allow(clippy::needless_borrow)] // Should not lint

src/tools/clippy/tests/ui/blanket_clippy_restriction_lints.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
error: `clippy::restriction` is not meant to be enabled as a group
2-
|
3-
= note: because of the command line `--warn clippy::restriction`
4-
= help: enable the restriction lints you need individually
5-
= note: `-D clippy::blanket-clippy-restriction-lints` implied by `-D warnings`
6-
= help: to override `-D warnings` add `#[allow(clippy::blanket_clippy_restriction_lints)]`
7-
81
error: `clippy::restriction` is not meant to be enabled as a group
92
--> $DIR/blanket_clippy_restriction_lints.rs:6:9
103
|
114
LL | #![warn(clippy::restriction)]
125
| ^^^^^^^^^^^^^^^^^^^
136
|
147
= help: enable the restriction lints you need individually
8+
= note: `-D clippy::blanket-clippy-restriction-lints` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::blanket_clippy_restriction_lints)]`
1510

1611
error: `clippy::restriction` is not meant to be enabled as a group
1712
--> $DIR/blanket_clippy_restriction_lints.rs:8:9
@@ -29,5 +24,10 @@ LL | #![forbid(clippy::restriction)]
2924
|
3025
= help: enable the restriction lints you need individually
3126

27+
error: `clippy::restriction` is not meant to be enabled as a group
28+
|
29+
= note: because of the command line `--warn clippy::restriction`
30+
= help: enable the restriction lints you need individually
31+
3232
error: aborting due to 4 previous errors
3333

0 commit comments

Comments
 (0)