Change lints_to_emit to lints_that_actually_run

This commit is contained in:
blyxyas 2024-05-27 13:58:33 +02:00
parent b4da058595
commit edc6577627
4 changed files with 27 additions and 23 deletions

View File

@ -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)
})

View File

@ -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<String>, Vec<String>)> {
pub fn lints_that_can_emit(tcx: TyCtxt<'_>, (): ()) -> Lrc<(FxIndexSet<String>, FxIndexSet<String>)> {
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<String>, Vec<Str
for group in lint_groups {
let binding = group.0.to_lowercase();
let group_name = name_without_tool(&binding).to_string();
if visitor.lints_to_emit.contains(&group_name) {
if visitor.lints_that_actually_run.contains(&group_name) {
for lint in group.1 {
visitor.lints_to_emit.push(name_without_tool(&lint.to_string()).to_string());
visitor.lints_that_actually_run.insert(name_without_tool(&lint.to_string()).to_string());
}
} else if visitor.lints_allowed.contains(&group_name) {
for lint in &group.1 {
visitor.lints_allowed.push(name_without_tool(&lint.to_string()).to_string());
visitor.lints_allowed.insert(name_without_tool(&lint.to_string()).to_string());
}
}
}
Lrc::new((visitor.lints_to_emit, visitor.lints_allowed))
Lrc::new((visitor.lints_that_actually_run, visitor.lints_allowed))
}
#[instrument(level = "trace", skip(tcx), ret)]
@ -339,26 +339,30 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> {
struct LintLevelMinimum<'tcx> {
tcx: TyCtxt<'tcx>,
/// The actual list of detected lints.
lints_to_emit: Vec<String>,
lints_allowed: Vec<String>,
lints_that_actually_run: FxIndexSet<String>,
lints_allowed: FxIndexSet<String>,
}
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());
}
}
}

View File

@ -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]);

View File

@ -422,7 +422,7 @@ rustc_queries! {
desc { "computing `#[expect]`ed lints in this crate" }
}
query lints_that_can_emit(_: ()) -> &'tcx Lrc<(Vec<String>, Vec<String>)> {
query lints_that_can_emit(_: ()) -> &'tcx Lrc<(FxIndexSet<String>, FxIndexSet<String>)> {
arena_cache
desc { "Computing all lints that are explicitly enabled or with a default level greater than Allow" }
}