Skip to content

Commit 30964bb

Browse files
Auto merge of #145220 - blyxyas:reverse-project-lint-filter, r=<try>
Improve incremental use of lint filtering
2 parents 18eeac0 + c5599e0 commit 30964bb

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

compiler/rustc_lint/src/levels.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
44
use rustc_data_structures::unord::UnordSet;
55
use rustc_errors::{Diag, LintDiagnostic, MultiSpan};
66
use rustc_feature::{Features, GateIssue};
7-
use rustc_hir::HirId;
87
use rustc_hir::intravisit::{self, Visitor};
8+
use rustc_hir::{HirId, OwnerId};
99
use rustc_index::IndexVec;
1010
use rustc_middle::bug;
1111
use rustc_middle::hir::nested_filter;
@@ -116,8 +116,29 @@ impl LintLevelSets {
116116
}
117117
}
118118

119+
fn lints_that_will_run_for_id(tcx: TyCtxt<'_>, key: OwnerId) -> Vec<LintId> {
120+
let shallow_lint_level = tcx.shallow_lint_levels_on(key);
121+
122+
let mut needs_to_run = Vec::new();
123+
124+
for (_, specs) in shallow_lint_level.specs.iter() {
125+
for (lint, level_and_source) in specs.iter() {
126+
if !matches!(level_and_source.level, Level::Allow) {
127+
needs_to_run.push(*lint)
128+
}
129+
}
130+
}
131+
132+
needs_to_run
133+
}
134+
119135
fn lints_that_dont_need_to_run(tcx: TyCtxt<'_>, (): ()) -> UnordSet<LintId> {
120136
let store = unerased_lint_store(&tcx.sess);
137+
138+
if tcx.sess.opts.lint_cap.is_some_and(|level| level == Level::Allow) {
139+
return UnordSet::from_iter(store.get_lints().into_iter().map(|lint| LintId::of(*lint)));
140+
}
141+
121142
let root_map = tcx.shallow_lint_levels_on(hir::CRATE_OWNER_ID);
122143

123144
let mut dont_need_to_run: FxHashSet<LintId> = store
@@ -141,15 +162,9 @@ fn lints_that_dont_need_to_run(tcx: TyCtxt<'_>, (): ()) -> UnordSet<LintId> {
141162
.collect();
142163

143164
for owner in tcx.hir_crate_items(()).owners() {
144-
let map = tcx.shallow_lint_levels_on(owner);
145-
146-
// All lints that appear with a non-allow level must be run.
147-
for (_, specs) in map.specs.iter() {
148-
for (lint, level_and_source) in specs.iter() {
149-
if !matches!(level_and_source.level, Level::Allow) {
150-
dont_need_to_run.remove(lint);
151-
}
152-
}
165+
let will_run = tcx.lints_that_will_run_for_id(owner);
166+
for lintid in will_run {
167+
dont_need_to_run.remove(lintid);
153168
}
154169
}
155170

@@ -1009,7 +1024,12 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
10091024
}
10101025

10111026
pub(crate) fn provide(providers: &mut Providers) {
1012-
*providers = Providers { shallow_lint_levels_on, lints_that_dont_need_to_run, ..*providers };
1027+
*providers = Providers {
1028+
shallow_lint_levels_on,
1029+
lints_that_dont_need_to_run,
1030+
lints_that_will_run_for_id,
1031+
..*providers
1032+
};
10131033
}
10141034

10151035
pub(crate) fn parse_lint_and_tool_name(lint_name: &str) -> (Option<Symbol>, &str) {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,11 @@ rustc_queries! {
556556
desc { "computing `#[expect]`ed lints in this crate" }
557557
}
558558

559+
query lints_that_will_run_for_id(key: hir::OwnerId) -> &'tcx Vec<LintId> {
560+
arena_cache
561+
desc { |tcx| "Computing lints that need to run {}", tcx.def_path_str(key) }
562+
}
563+
559564
query lints_that_dont_need_to_run(_: ()) -> &'tcx UnordSet<LintId> {
560565
arena_cache
561566
desc { "Computing all lints that are explicitly enabled or with a default level greater than Allow" }

0 commit comments

Comments
 (0)