rust/compiler/rustc_lint/src/expect.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.8 KiB
Rust
Raw Normal View History

2024-08-31 14:32:15 +00:00
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::CRATE_OWNER_ID;
use rustc_middle::lint::LintExpectation;
use rustc_middle::query::Providers;
2022-11-09 23:22:48 +00:00
use rustc_middle::ty::TyCtxt;
2024-08-31 14:32:15 +00:00
use rustc_session::lint::LintExpectationId;
2022-11-09 23:22:48 +00:00
use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
use rustc_span::Symbol;
use crate::lints::{Expectation, ExpectationNote};
pub(crate) fn provide(providers: &mut Providers) {
*providers = Providers { lint_expectations, check_expectations, ..*providers };
}
fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExpectation)> {
let krate = tcx.hir_crate_items(());
let mut expectations = Vec::new();
2024-08-31 14:32:15 +00:00
for owner in std::iter::once(CRATE_OWNER_ID).chain(krate.owners()) {
let lints = tcx.shallow_lint_levels_on(owner);
expectations.extend_from_slice(&lints.expectations);
}
expectations
}
fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
2022-07-22 16:48:36 +00:00
let lint_expectations = tcx.lint_expectations(());
let fulfilled_expectations = tcx.dcx().steal_fulfilled_expectation_ids();
2022-07-22 16:48:36 +00:00
2024-08-31 14:32:15 +00:00
// Turn a `LintExpectationId` into a `(AttrId, lint_index)` pair.
let canonicalize_id = |expect_id: &LintExpectationId| {
match *expect_id {
LintExpectationId::Unstable { attr_id, lint_index: Some(lint_index) } => {
(attr_id, lint_index)
}
LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
// We are an `eval_always` query, so looking at the attribute's `AttrId` is ok.
let attr_id = tcx.hir().attrs(hir_id)[attr_index as usize].id;
(attr_id, lint_index)
}
2024-08-31 14:32:15 +00:00
_ => panic!("fulfilled expectations must have a lint index"),
}
};
let fulfilled_expectations: FxHashSet<_> =
fulfilled_expectations.iter().map(canonicalize_id).collect();
for (expect_id, expectation) in lint_expectations {
// This check will always be true, since `lint_expectations` only holds stable ids
let LintExpectationId::Stable { hir_id, .. } = expect_id else {
unreachable!("at this stage all `LintExpectationId`s are stable");
2024-08-31 14:32:15 +00:00
};
let expect_id = canonicalize_id(expect_id);
if !fulfilled_expectations.contains(&expect_id)
&& tool_filter.map_or(true, |filter| expectation.lint_tool == Some(filter))
{
let rationale = expectation.reason.map(|rationale| ExpectationNote { rationale });
let note = expectation.is_unfulfilled_lint_expectations;
tcx.emit_node_span_lint(
UNFULFILLED_LINT_EXPECTATIONS,
*hir_id,
expectation.emission_span,
Expectation { rationale, note },
);
}
}
}