1use rustc_data_structures::fx::FxHashSet;
2use rustc_middle::lint::LintExpectation;
3use rustc_middle::query::Providers;
4use rustc_middle::ty::TyCtxt;
5use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
6use rustc_session::lint::{LintExpectationId, StableLintExpectationId};
7use rustc_span::Symbol;
8
9use crate::lints::{Expectation, ExpectationNote};
10
11pub(crate) fn provide(providers: &mut Providers) {
12 *providers = Providers { lint_expectations, check_expectations, ..*providers };
13}
14
15fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(StableLintExpectationId, LintExpectation)> {
16 let krate = tcx.hir_crate_items(());
17
18 let mut expectations = Vec::new();
19
20 for owner in krate.owners() {
21 let lints = tcx.shallow_lint_levels_on(owner);
22 expectations.extend_from_slice(&lints.expectations);
23 }
24
25 expectations
26}
27
28fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
29 let lint_expectations = tcx.lint_expectations(());
30 let fulfilled_expectations = tcx.dcx().steal_fulfilled_expectation_ids();
31
32 let canonicalize_id = |expect_id: &LintExpectationId| {
34 let (attr_id, lint_index) = match *expect_id {
35 LintExpectationId::Unstable(id) => (id.attr_id, id.lint_index),
36 LintExpectationId::Stable(id) => {
37 (tcx.hir_attrs(id.hir_id)[id.attr_index as usize].id(), id.lint_index)
39 }
40 };
41 (attr_id, lint_index.expect("fulfilled expectations must have a lint index"))
42 };
43
44 let fulfilled_expectations: FxHashSet<_> =
45 fulfilled_expectations.iter().map(canonicalize_id).collect();
46
47 for (expect_id, expectation) in lint_expectations {
48 let hir_id = expect_id.hir_id;
49 let expect_id = canonicalize_id(&LintExpectationId::Stable(*expect_id));
50
51 if !fulfilled_expectations.contains(&expect_id)
52 && tool_filter.is_none_or(|filter| expectation.lint_tool == Some(filter))
53 {
54 let rationale = expectation.reason.map(|rationale| ExpectationNote { rationale });
55 let note = expectation.is_unfulfilled_lint_expectations;
56 tcx.emit_node_span_lint(
57 UNFULFILLED_LINT_EXPECTATIONS,
58 hir_id,
59 expectation.emission_span,
60 Expectation { rationale, note },
61 );
62 }
63 }
64}