Skip to content

Commit 942ac9c

Browse files
committed
Auto merge of #157689 - nnethercote:lint-cleanups, r=cjgillot
Lint cleanups Details in individual commits. r? @GuillaumeGomez
2 parents e238223 + 618f5cd commit 942ac9c

7 files changed

Lines changed: 55 additions & 79 deletions

File tree

compiler/rustc_lint/src/context.rs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ use self::TargetLint::*;
4040
use crate::levels::LintLevelsBuilder;
4141
use crate::passes::{EarlyLintPassObject, LateLintPassObject};
4242

43-
type EarlyLintPassFactory = dyn Fn() -> EarlyLintPassObject + sync::DynSend + sync::DynSync;
43+
type EarlyLintPassFactory = Box<dyn Fn() -> EarlyLintPassObject + sync::DynSend + sync::DynSync>;
4444
type LateLintPassFactory =
45-
dyn for<'tcx> Fn(TyCtxt<'tcx>) -> LateLintPassObject<'tcx> + sync::DynSend + sync::DynSync;
45+
Box<dyn for<'tcx> Fn(TyCtxt<'tcx>) -> LateLintPassObject<'tcx> + sync::DynSend + sync::DynSync>;
4646

4747
/// Information about the registered lints.
4848
pub struct LintStore {
@@ -55,11 +55,11 @@ pub struct LintStore {
5555
/// interior mutability, we don't enforce this (and lints should, in theory,
5656
/// be compatible with being constructed more than once, though not
5757
/// necessarily in a sane manner. This is safe though.)
58-
pub pre_expansion_passes: Vec<Box<EarlyLintPassFactory>>,
59-
pub early_passes: Vec<Box<EarlyLintPassFactory>>,
60-
pub late_passes: Vec<Box<LateLintPassFactory>>,
58+
pub pre_expansion_passes: Vec<EarlyLintPassFactory>,
59+
pub early_passes: Vec<EarlyLintPassFactory>,
60+
pub late_passes: Vec<LateLintPassFactory>,
6161
/// This is unique in that we construct them per-module, so not once.
62-
pub late_module_passes: Vec<Box<LateLintPassFactory>>,
62+
pub late_module_passes: Vec<LateLintPassFactory>,
6363

6464
/// Lints indexed by name.
6565
by_name: UnordMap<String, TargetLint>,
@@ -165,11 +165,8 @@ impl LintStore {
165165
self.lint_groups.keys().copied()
166166
}
167167

168-
pub fn register_early_pass(
169-
&mut self,
170-
pass: impl Fn() -> EarlyLintPassObject + 'static + sync::DynSend + sync::DynSync,
171-
) {
172-
self.early_passes.push(Box::new(pass));
168+
pub fn register_early_pass(&mut self, pass: EarlyLintPassFactory) {
169+
self.early_passes.push(pass);
173170
}
174171

175172
/// This lint pass is softly deprecated. It misses expanded code and has caused a few
@@ -178,31 +175,16 @@ impl LintStore {
178175
///
179176
/// * See [rust#69838](https://github.com/rust-lang/rust/pull/69838)
180177
/// * See [rust-clippy#5518](https://github.com/rust-lang/rust-clippy/pull/5518)
181-
pub fn register_pre_expansion_pass(
182-
&mut self,
183-
pass: impl Fn() -> EarlyLintPassObject + 'static + sync::DynSend + sync::DynSync,
184-
) {
185-
self.pre_expansion_passes.push(Box::new(pass));
178+
pub fn register_pre_expansion_pass(&mut self, pass: EarlyLintPassFactory) {
179+
self.pre_expansion_passes.push(pass);
186180
}
187181

188-
pub fn register_late_pass(
189-
&mut self,
190-
pass: impl for<'tcx> Fn(TyCtxt<'tcx>) -> LateLintPassObject<'tcx>
191-
+ 'static
192-
+ sync::DynSend
193-
+ sync::DynSync,
194-
) {
195-
self.late_passes.push(Box::new(pass));
182+
pub fn register_late_pass(&mut self, pass: LateLintPassFactory) {
183+
self.late_passes.push(pass);
196184
}
197185

198-
pub fn register_late_mod_pass(
199-
&mut self,
200-
pass: impl for<'tcx> Fn(TyCtxt<'tcx>) -> LateLintPassObject<'tcx>
201-
+ 'static
202-
+ sync::DynSend
203-
+ sync::DynSync,
204-
) {
205-
self.late_module_passes.push(Box::new(pass));
186+
pub fn register_late_mod_pass(&mut self, pass: LateLintPassFactory) {
187+
self.late_module_passes.push(pass);
206188
}
207189

208190
/// Helper method for register_early/late_pass

compiler/rustc_lint/src/early.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ impl<'ast, 'ecx, T: EarlyLintPass> ast_visit::Visitor<'ast> for EarlyContextAndP
195195
}
196196

197197
fn visit_where_predicate(&mut self, p: &'ast ast::WherePredicate) {
198-
lint_callback!(self, enter_where_predicate, p);
198+
lint_callback!(self, check_where_predicate, p);
199199
ast_visit::walk_where_predicate(self, p);
200-
lint_callback!(self, exit_where_predicate, p);
200+
lint_callback!(self, check_where_predicate_post, p);
201201
}
202202

203203
fn visit_poly_trait_ref(&mut self, t: &'ast ast::PolyTraitRef) {
@@ -246,12 +246,12 @@ impl<'ast, 'ecx, T: EarlyLintPass> ast_visit::Visitor<'ast> for EarlyContextAndP
246246
// `check_foo` method in `$methods` within this pass simply calls `check_foo`
247247
// once per `$pass`. Compare with `declare_combined_early_lint_pass`, which is
248248
// similar, but combines lint passes at compile time.
249-
struct RuntimeCombinedEarlyLintPass<'a> {
250-
passes: &'a mut [EarlyLintPassObject],
249+
struct RuntimeCombinedEarlyLintPass {
250+
passes: Vec<EarlyLintPassObject>,
251251
}
252252

253253
#[allow(rustc::lint_pass_impl_without_macro)]
254-
impl LintPass for RuntimeCombinedEarlyLintPass<'_> {
254+
impl LintPass for RuntimeCombinedEarlyLintPass {
255255
fn name(&self) -> &'static str {
256256
panic!()
257257
}
@@ -262,7 +262,7 @@ impl LintPass for RuntimeCombinedEarlyLintPass<'_> {
262262

263263
macro_rules! impl_early_lint_pass {
264264
([], [$($(#[$attr:meta])* fn $f:ident($($param:ident: $arg:ty),*);)*]) => (
265-
impl EarlyLintPass for RuntimeCombinedEarlyLintPass<'_> {
265+
impl EarlyLintPass for RuntimeCombinedEarlyLintPass {
266266
$(fn $f(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
267267
for pass in self.passes.iter_mut() {
268268
pass.$f(context, $($param),*);
@@ -338,7 +338,7 @@ pub fn check_ast_node<'a>(
338338
} else {
339339
let mut passes: Vec<_> = passes.iter().map(|mk_pass| (mk_pass)()).collect();
340340
passes.push(Box::new(builtin_lints));
341-
let pass = RuntimeCombinedEarlyLintPass { passes: &mut passes[..] };
341+
let pass = RuntimeCombinedEarlyLintPass { passes };
342342
check_ast_node_inner(sess, check_node, context, pass);
343343
}
344344
}

compiler/rustc_lint/src/late.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,12 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
305305
// `check_foo` method in `$methods` within this pass simply calls `check_foo`
306306
// once per `$pass`. Compare with `declare_combined_late_lint_pass`, which is
307307
// similar, but combines lint passes at compile time.
308-
struct RuntimeCombinedLateLintPass<'a, 'tcx> {
309-
passes: &'a mut [LateLintPassObject<'tcx>],
308+
struct RuntimeCombinedLateLintPass<'tcx> {
309+
passes: Vec<LateLintPassObject<'tcx>>,
310310
}
311311

312312
#[allow(rustc::lint_pass_impl_without_macro)]
313-
impl LintPass for RuntimeCombinedLateLintPass<'_, '_> {
313+
impl LintPass for RuntimeCombinedLateLintPass<'_> {
314314
fn name(&self) -> &'static str {
315315
panic!()
316316
}
@@ -321,7 +321,7 @@ impl LintPass for RuntimeCombinedLateLintPass<'_, '_> {
321321

322322
macro_rules! impl_late_lint_pass {
323323
([], [$($(#[$attr:meta])* fn $f:ident($($param:ident: $arg:ty),*);)*]) => {
324-
impl<'tcx> LateLintPass<'tcx> for RuntimeCombinedLateLintPass<'_, 'tcx> {
324+
impl<'tcx> LateLintPass<'tcx> for RuntimeCombinedLateLintPass<'tcx> {
325325
$(fn $f(&mut self, context: &LateContext<'tcx>, $($param: $arg),*) {
326326
for pass in self.passes.iter_mut() {
327327
pass.$f(context, $($param),*);
@@ -367,14 +367,14 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
367367
}
368368
} else {
369369
let builtin_lints = Box::new(builtin_lints) as Box<dyn LateLintPass<'tcx>>;
370-
let mut binding = store
370+
let passes = store
371371
.late_module_passes
372372
.iter()
373373
.map(|mk_pass| (mk_pass)(tcx))
374374
.chain(std::iter::once(builtin_lints))
375375
.collect::<Vec<_>>();
376376

377-
let pass = RuntimeCombinedLateLintPass { passes: binding.as_mut_slice() };
377+
let pass = RuntimeCombinedLateLintPass { passes };
378378
late_lint_mod_inner(tcx, module_def_id, context, pass);
379379
}
380380
}
@@ -404,10 +404,18 @@ fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>(
404404
}
405405

406406
fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
407-
// Note: `passes` is often empty.
408-
let passes: Vec<_> =
409-
unerased_lint_store(tcx.sess).late_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();
407+
let lints_that_dont_need_to_run = tcx.lints_that_dont_need_to_run(());
410408

409+
// Note: `passes` is often empty after filtering.
410+
let mut passes: Vec<_> =
411+
unerased_lint_store(tcx.sess).late_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();
412+
passes.retain(|pass| {
413+
let lints = pass.get_lints();
414+
// Lintless passes are always in
415+
lints.is_empty() ||
416+
// If the pass doesn't have a single needed lint, omit it
417+
!lints.iter().all(|lint| lints_that_dont_need_to_run.contains(&LintId::of(lint)))
418+
});
411419
if passes.is_empty() {
412420
return;
413421
}
@@ -423,20 +431,7 @@ fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
423431
only_module: false,
424432
};
425433

426-
let lints_that_dont_need_to_run = tcx.lints_that_dont_need_to_run(());
427-
428-
let mut filtered_passes: Vec<Box<dyn LateLintPass<'tcx>>> = passes
429-
.into_iter()
430-
.filter(|pass| {
431-
let lints = (**pass).get_lints();
432-
// Lintless passes are always in
433-
lints.is_empty() ||
434-
// If the pass doesn't have a single needed lint, omit it
435-
!lints.iter().all(|lint| lints_that_dont_need_to_run.contains(&LintId::of(lint)))
436-
})
437-
.collect();
438-
439-
let pass = RuntimeCombinedLateLintPass { passes: &mut filtered_passes[..] };
434+
let pass = RuntimeCombinedLateLintPass { passes };
440435
let mut cx = LateContextAndPass { context, pass };
441436

442437
// Visit the whole crate.

compiler/rustc_lint/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,10 @@ fn register_builtins(store: &mut LintStore) {
694694

695695
fn register_internals(store: &mut LintStore) {
696696
store.register_lints(&InternalCombinedEarlyLintPass::lint_vec());
697-
store.register_early_pass(|| Box::new(InternalCombinedEarlyLintPass::new()));
697+
store.register_early_pass(Box::new(|| Box::new(InternalCombinedEarlyLintPass::new())));
698698

699699
store.register_lints(&InternalCombinedModuleLateLintPass::lint_vec());
700-
store.register_late_mod_pass(|_| Box::new(InternalCombinedModuleLateLintPass::new()));
700+
store.register_late_mod_pass(Box::new(|_| Box::new(InternalCombinedModuleLateLintPass::new())));
701701

702702
store.register_group(
703703
false,

compiler/rustc_lint/src/passes.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::context::{EarlyContext, LateContext};
55
#[macro_export]
66
macro_rules! late_lint_methods {
77
($macro:path, $args:tt) => (
8+
// `_post` methods are called *after* recursing into the node.
89
$macro!($args, [
910
fn check_body(a: &rustc_hir::Body<'tcx>);
1011
fn check_body_post(a: &rustc_hir::Body<'tcx>);
@@ -13,8 +14,6 @@ macro_rules! late_lint_methods {
1314
fn check_mod(a: &'tcx rustc_hir::Mod<'tcx>, b: rustc_hir::HirId);
1415
fn check_foreign_item(a: &'tcx rustc_hir::ForeignItem<'tcx>);
1516
fn check_item(a: &'tcx rustc_hir::Item<'tcx>);
16-
/// This is called *after* recursing into the item
17-
/// (in contrast to `check_item`, which is checked before).
1817
fn check_item_post(a: &'tcx rustc_hir::Item<'tcx>);
1918
fn check_local(a: &'tcx rustc_hir::LetStmt<'tcx>);
2019
fn check_block(a: &'tcx rustc_hir::Block<'tcx>);
@@ -59,7 +58,7 @@ macro_rules! late_lint_methods {
5958
// contains a few lint-specific methods with no equivalent in `Visitor`.
6059
//
6160
macro_rules! declare_late_lint_pass {
62-
([], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
61+
([], [$(fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
6362
pub trait LateLintPass<'tcx>: LintPass {
6463
$(#[inline(always)] fn $name(&mut self, _: &LateContext<'tcx>, $(_: $arg),*) {})*
6564
}
@@ -79,7 +78,7 @@ macro_rules! expand_combined_late_lint_pass_method {
7978

8079
#[macro_export]
8180
macro_rules! expand_combined_late_lint_pass_methods {
82-
($passes:tt, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
81+
($passes:tt, [$(fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
8382
$(fn $name(&mut self, context: &$crate::LateContext<'tcx>, $($param: $arg),*) {
8483
$crate::expand_combined_late_lint_pass_method!($passes, self, $name, (context, $($param),*));
8584
})*
@@ -132,14 +131,13 @@ macro_rules! declare_combined_late_lint_pass {
132131
#[macro_export]
133132
macro_rules! early_lint_methods {
134133
($macro:path, $args:tt) => (
134+
// `_post` methods are called *after* recursing into the node.
135135
$macro!($args, [
136136
fn check_param(a: &rustc_ast::Param);
137137
fn check_ident(a: &rustc_span::Ident);
138138
fn check_crate(a: &rustc_ast::Crate);
139139
fn check_crate_post(a: &rustc_ast::Crate);
140140
fn check_item(a: &rustc_ast::Item);
141-
/// This is called *after* recursing into the item
142-
/// (in contrast to `check_item`, which is checked before).
143141
fn check_item_post(a: &rustc_ast::Item);
144142
fn check_local(a: &rustc_ast::Local);
145143
fn check_block(a: &rustc_ast::Block);
@@ -168,15 +166,14 @@ macro_rules! early_lint_methods {
168166
fn check_attributes_post(a: &[rustc_ast::Attribute]);
169167
fn check_mac_def(a: &rustc_ast::MacroDef);
170168
fn check_mac(a: &rustc_ast::MacCall);
171-
172-
fn enter_where_predicate(a: &rustc_ast::WherePredicate);
173-
fn exit_where_predicate(a: &rustc_ast::WherePredicate);
169+
fn check_where_predicate(a: &rustc_ast::WherePredicate);
170+
fn check_where_predicate_post(a: &rustc_ast::WherePredicate);
174171
]);
175172
)
176173
}
177174

178175
macro_rules! declare_early_lint_pass {
179-
([], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
176+
([], [$(fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
180177
pub trait EarlyLintPass: LintPass {
181178
$(#[inline(always)] fn $name(&mut self, _: &EarlyContext<'_>, $(_: $arg),*) {})*
182179
}
@@ -196,7 +193,7 @@ macro_rules! expand_combined_early_lint_pass_method {
196193

197194
#[macro_export]
198195
macro_rules! expand_combined_early_lint_pass_methods {
199-
($passes:tt, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
196+
($passes:tt, [$(fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
200197
$(fn $name(&mut self, context: &$crate::EarlyContext<'_>, $($param: $arg),*) {
201198
$crate::expand_combined_early_lint_pass_method!($passes, self, $name, (context, $($param),*));
202199
})*
@@ -247,5 +244,5 @@ macro_rules! declare_combined_early_lint_pass {
247244
}
248245

249246
/// A lint pass boxed up as a trait object.
250-
pub(crate) type EarlyLintPassObject = Box<dyn EarlyLintPass + 'static>;
247+
pub(crate) type EarlyLintPassObject = Box<dyn EarlyLintPass>;
251248
pub(crate) type LateLintPassObject<'tcx> = Box<dyn LateLintPass<'tcx> + 'tcx>;

compiler/rustc_lint/src/unused.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ impl EarlyLintPass for UnusedParens {
975975
self.in_no_bounds_pos.clear();
976976
}
977977

978-
fn enter_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
978+
fn check_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
979979
use rustc_ast::{WhereBoundPredicate, WherePredicateKind};
980980
if let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
981981
bounded_ty,
@@ -989,7 +989,7 @@ impl EarlyLintPass for UnusedParens {
989989
}
990990
}
991991

992-
fn exit_where_predicate(&mut self, _: &EarlyContext<'_>, _: &ast::WherePredicate) {
992+
fn check_where_predicate_post(&mut self, _: &EarlyContext<'_>, _: &ast::WherePredicate) {
993993
assert!(!self.with_self_ty_parens);
994994
}
995995
}

src/tools/clippy/clippy_lints/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,9 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
454454
// NOTE: Do not add any more pre-expansion passes. These should be removed eventually.
455455
// Due to the architecture of the compiler, currently `cfg_attr` attributes on crate
456456
// level (i.e `#![cfg_attr(...)]`) will still be expanded even when using a pre-expansion pass.
457-
store.register_pre_expansion_pass(move || Box::new(attrs::EarlyAttributes::new(conf)));
457+
store.register_pre_expansion_pass(
458+
Box::new(move || Box::new(attrs::EarlyAttributes::new(conf)))
459+
);
458460

459461
let format_args_storage = FormatArgsStorage::default();
460462
let attr_storage = AttrStorage::default();

0 commit comments

Comments
 (0)