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::LintExpectationId;
6use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
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<(LintExpectationId, 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 match *expect_id {
35 LintExpectationId::Unstable { attr_id, lint_index: Some(lint_index) } => {
36 (attr_id, lint_index)
37 }
38 LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
39 let attr_id = tcx.hir_attrs(hir_id)[attr_index as usize].id();
41
42 (attr_id, lint_index)
43 }
44 _ => panic!("fulfilled expectations must have a lint index"),
45 }
46 };
47
48 let fulfilled_expectations: FxHashSet<_> =
49 fulfilled_expectations.iter().map(canonicalize_id).collect();
50
51 for (expect_id, expectation) in lint_expectations {
52 let LintExpectationId::Stable { hir_id, .. } = expect_id else {
54 unreachable!("at this stage all `LintExpectationId`s are stable");
55 };
56
57 let expect_id = canonicalize_id(expect_id);
58
59 if !fulfilled_expectations.contains(&expect_id)
60 && tool_filter.is_none_or(|filter| expectation.lint_tool == Some(filter))
61 {
62 let rationale = expectation.reason.map(|rationale| ExpectationNote { rationale });
63 let note = expectation.is_unfulfilled_lint_expectations;
64 tcx.emit_node_span_lint(
65 UNFULFILLED_LINT_EXPECTATIONS,
66 *hir_id,
67 expectation.emission_span,
68 Expectation { rationale, note },
69 );
70 }
71 }
72}