From edc65776274d14fc7f7f93de66ac3b2d15bbbc37 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Mon, 27 May 2024 13:58:33 +0200 Subject: [PATCH] Change lints_to_emit to lints_that_actually_run --- compiler/rustc_lint/src/late.rs | 6 +-- compiler/rustc_lint/src/levels.rs | 40 ++++++++++--------- compiler/rustc_lint/src/shadowed_into_iter.rs | 2 +- compiler/rustc_middle/src/query/mod.rs | 2 +- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_lint/src/late.rs b/compiler/rustc_lint/src/late.rs index ccd06ba612b..c7187ee1b30 100644 --- a/compiler/rustc_lint/src/late.rs +++ b/compiler/rustc_lint/src/late.rs @@ -373,8 +373,8 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>( store.late_module_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect(); // Filter unused lints - let (lints_to_emit, lints_allowed) = &**tcx.lints_that_can_emit(()); - // let lints_to_emit = &lints_that_can_emit.0; + 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. @@ -386,7 +386,7 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>( let pass = LintPass::get_lints(pass); pass.iter().any(|&lint| { let lint_name = name_without_tool(&lint.name.to_lowercase()).to_string(); - lints_to_emit.contains(&lint_name) + lints_that_actually_run.contains(&lint_name) || (!lints_allowed.contains(&lint_name) && lint.default_level != crate::Level::Allow) }) diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 7ee8618bc55..527fc117351 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -1,6 +1,6 @@ use rustc_ast_pretty::pprust; -use rustc_data_structures::{fx::FxIndexMap, sync::Lrc}; -use rustc_errors::{Diag, DiagMessage, LintDiagnostic, MultiSpan}; +use rustc_data_structures::{fx::FxIndexMap, fx::FxIndexSet, sync::Lrc}; +use rustc_errors::{Diag, LintDiagnostic, MultiSpan}; use rustc_feature::{Features, GateIssue}; use rustc_hir::HirId; use rustc_hir::intravisit::{self, Visitor}; @@ -120,7 +120,7 @@ impl LintLevelSets { /// (and not allowed in the crate) and CLI lints. The returned value is a tuple /// of 1. The lints that will emit (or at least, should run), and 2. /// The lints that are allowed at the crate level and will not emit. -pub fn lints_that_can_emit(tcx: TyCtxt<'_>, (): ()) -> Lrc<(Vec, Vec)> { +pub fn lints_that_can_emit(tcx: TyCtxt<'_>, (): ()) -> Lrc<(FxIndexSet, FxIndexSet)> { let mut visitor = LintLevelMinimum::new(tcx); visitor.process_opts(); tcx.hir().walk_attributes(&mut visitor); @@ -131,18 +131,18 @@ pub fn lints_that_can_emit(tcx: TyCtxt<'_>, (): ()) -> Lrc<(Vec, Vec Visitor<'tcx> for LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> { struct LintLevelMinimum<'tcx> { tcx: TyCtxt<'tcx>, /// The actual list of detected lints. - lints_to_emit: Vec, - lints_allowed: Vec, + lints_that_actually_run: FxIndexSet, + lints_allowed: FxIndexSet, } impl<'tcx> LintLevelMinimum<'tcx> { pub fn new(tcx: TyCtxt<'tcx>) -> Self { + let mut lints_that_actually_run = FxIndexSet::default(); + lints_that_actually_run.reserve(230); + let mut lints_allowed = FxIndexSet::default(); + lints_allowed.reserve(100); Self { tcx, // That magic number is the current number of lints + some more for possible future lints - lints_to_emit: Vec::with_capacity(230), - lints_allowed: Vec::with_capacity(100), + lints_that_actually_run, + lints_allowed, } } fn process_opts(&mut self) { for (lint, level) in &self.tcx.sess.opts.lint_opts { if *level == Level::Allow { - self.lints_allowed.push(lint.clone()); + self.lints_allowed.insert(lint.clone()); } else { - self.lints_to_emit.push(lint.to_string()); + self.lints_that_actually_run.insert(lint.to_string()); } } } @@ -383,13 +387,13 @@ impl<'tcx> Visitor<'tcx> for LintLevelMinimum<'tcx> { // If it's a tool lint (e.g. clippy::my_clippy_lint) if let ast::NestedMetaItem::MetaItem(meta_item) = meta_list { if meta_item.path.segments.len() == 1 { - self.lints_to_emit.push( + self.lints_that_actually_run.insert( // SAFETY: Lint attributes can only have literals meta_list.ident().unwrap().name.as_str().to_string(), ); } else { - self.lints_to_emit - .push(meta_item.path.segments[1].ident.name.as_str().to_string()); + self.lints_that_actually_run + .insert(meta_item.path.segments[1].ident.name.as_str().to_string()); } } } @@ -401,10 +405,10 @@ impl<'tcx> Visitor<'tcx> for LintLevelMinimum<'tcx> { // If it's a tool lint (e.g. clippy::my_clippy_lint) if let ast::NestedMetaItem::MetaItem(meta_item) = meta_list { if meta_item.path.segments.len() == 1 { - self.lints_allowed.push(meta_list.name_or_empty().as_str().to_string()) + self.lints_allowed.insert(meta_list.name_or_empty().as_str().to_string()); } else { self.lints_allowed - .push(meta_item.path.segments[1].ident.name.as_str().to_string()); + .insert(meta_item.path.segments[1].ident.name.as_str().to_string()); } } } diff --git a/compiler/rustc_lint/src/shadowed_into_iter.rs b/compiler/rustc_lint/src/shadowed_into_iter.rs index a73904cd776..33a4ae60663 100644 --- a/compiler/rustc_lint/src/shadowed_into_iter.rs +++ b/compiler/rustc_lint/src/shadowed_into_iter.rs @@ -64,7 +64,7 @@ declare_lint! { }; } -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Default)] pub(crate) struct ShadowedIntoIter; impl_lint_pass!(ShadowedIntoIter => [ARRAY_INTO_ITER, BOXED_SLICE_INTO_ITER]); diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index e90c3a4c23d..b6cb11a2a2c 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -422,7 +422,7 @@ rustc_queries! { desc { "computing `#[expect]`ed lints in this crate" } } - query lints_that_can_emit(_: ()) -> &'tcx Lrc<(Vec, Vec)> { + query lints_that_can_emit(_: ()) -> &'tcx Lrc<(FxIndexSet, FxIndexSet)> { arena_cache desc { "Computing all lints that are explicitly enabled or with a default level greater than Allow" } }