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 mut inner_expectations = Vec::new();
23 let lints = tcx.shallow_lint_levels_on(owner);
24 for expectation in &lints.expectations {
25 let canonicalized = canonicalize_id(&expectation.0);
26 if !inner_expectations.iter().any(|(id, _)| canonicalize_id(id) == canonicalized) {
27 inner_expectations.push(expectation.clone());
28 }
29 }
30 expectations.extend(inner_expectations);
31 }
32
33 expectations
34}
35
36fn canonicalize_id(expect_id: &LintExpectationId) -> (rustc_span::AttrId, u16) {
37 match *expect_id {
38 LintExpectationId::Unstable { attr_id, lint_index, .. } => (attr_id, lint_index),
39 LintExpectationId::Stable { attr_id, lint_index, .. } => (attr_id, lint_index),
40 }
41}
42
43fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
44 let lint_expectations = tcx.lint_expectations(());
45 let fulfilled_expectations = tcx.dcx().steal_fulfilled_expectation_ids();
46
47 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 {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("at this stage all `LintExpectationId`s are stable")));
};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}