diff options
| author | blyxyas <blyxyas@gmail.com> | 2024-06-18 22:44:28 +0200 |
|---|---|---|
| committer | blyxyas <blyxyas@gmail.com> | 2024-10-19 16:20:33 +0200 |
| commit | 71b4d108c7e71c15c0f61d737ebdc0e1cf559d3c (patch) | |
| tree | 2554ae8686bc2a11446fe6c34f5ed99311a4f8ac /compiler/rustc_lint/src/late.rs | |
| parent | edc65776274d14fc7f7f93de66ac3b2d15bbbc37 (diff) | |
| download | rust-71b4d108c7e71c15c0f61d737ebdc0e1cf559d3c.tar.gz rust-71b4d108c7e71c15c0f61d737ebdc0e1cf559d3c.zip | |
Follow review comments (optimize the filtering)
Diffstat (limited to 'compiler/rustc_lint/src/late.rs')
| -rw-r--r-- | compiler/rustc_lint/src/late.rs | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/compiler/rustc_lint/src/late.rs b/compiler/rustc_lint/src/late.rs index c7187ee1b30..f8bd873cdf5 100644 --- a/compiler/rustc_lint/src/late.rs +++ b/compiler/rustc_lint/src/late.rs @@ -24,13 +24,12 @@ use rustc_hir::def_id::{LocalDefId, LocalModDefId}; use rustc_hir::{HirId, intravisit as hir_visit}; use rustc_middle::hir::nested_filter; use rustc_middle::ty::{self, TyCtxt}; -use rustc_session::Session; -use rustc_session::lint::LintPass; +use rustc_session::{Session, lint::{LintPass, builtin::HardwiredLints}}; use rustc_span::Span; use tracing::debug; use crate::passes::LateLintPassObject; -use crate::{LateContext, LateLintPass, LintStore}; +use crate::{LateContext, LateLintPass, LintId, LintStore}; /// Extract the [`LintStore`] from [`Session`]. /// @@ -371,29 +370,24 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>( } else { let passes: Vec<_> = store.late_module_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect(); - // Filter unused lints - let (lints_that_actually_run, lints_allowed) = &**tcx.lints_that_can_emit(()); - // let lints_that_actually_run = &lints_that_can_emit.0; - // let lints_allowed = &lints_that_can_emit.1; - - // Now, we'll filtered passes in a way that discards any lint that won't trigger. - // If any lint is a given pass is detected to be emitted, we will keep that pass. - // Otherwise, we don't + let lints_that_dont_need_to_run = tcx.lints_that_dont_need_to_run(()); let mut filtered_passes: Vec<Box<dyn LateLintPass<'tcx>>> = passes .into_iter() .filter(|pass| { - let pass = LintPass::get_lints(pass); - pass.iter().any(|&lint| { - let lint_name = name_without_tool(&lint.name.to_lowercase()).to_string(); - lints_that_actually_run.contains(&lint_name) - || (!lints_allowed.contains(&lint_name) - && lint.default_level != crate::Level::Allow) - }) + let lints = LintPass::get_lints(pass); + if lints.is_empty() { + true + } else { + lints + .iter() + .any(|lint| !lints_that_dont_need_to_run.contains(&LintId::of(lint))) + } }) .collect(); filtered_passes.push(Box::new(builtin_lints)); + filtered_passes.push(Box::new(HardwiredLints)); let pass = RuntimeCombinedLateLintPass { passes: &mut filtered_passes[..] }; late_lint_mod_inner(tcx, module_def_id, context, pass); @@ -426,7 +420,7 @@ fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>( fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) { // Note: `passes` is often empty. - let mut passes: Vec<_> = + let passes: Vec<_> = unerased_lint_store(tcx.sess).late_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect(); if passes.is_empty() { @@ -444,7 +438,30 @@ fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) { only_module: false, }; - let pass = RuntimeCombinedLateLintPass { passes: &mut passes[..] }; + let lints_that_dont_need_to_run = tcx.lints_that_dont_need_to_run(()); + + // dbg!(&lints_that_dont_need_to_run); + let mut filtered_passes: Vec<Box<dyn LateLintPass<'tcx>>> = passes + .into_iter() + .filter(|pass| { + let lints = LintPass::get_lints(pass); + !lints.iter().all(|lint| lints_that_dont_need_to_run.contains(&LintId::of(lint))) + }) + .collect(); + + filtered_passes.push(Box::new(HardwiredLints)); + + // let mut filtered_passes: Vec<Box<dyn LateLintPass<'tcx>>> = passes + // .into_iter() + // .filter(|pass| { + // let lints = LintPass::get_lints(pass); + // lints.iter() + // .any(|lint| + // !lints_that_dont_need_to_run.contains(&LintId::of(lint))) + // }).collect(); + // + + let pass = RuntimeCombinedLateLintPass { passes: &mut filtered_passes[..] }; late_lint_crate_inner(tcx, context, pass); } @@ -482,10 +499,3 @@ pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>) { }, ); } - -/// Format name ignoring the name, useful for filtering non-used lints. -/// For example, 'clippy::my_lint' will turn into 'my_lint' -pub(crate) fn name_without_tool(name: &str) -> &str { - // Doing some calculations here to account for those separators - name.rsplit("::").next().unwrap_or(name) -} |
