]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/expect.rs
Rollup merge of #106779 - RReverser:patch-2, r=Mark-Simulacrum
[rust.git] / compiler / rustc_lint / src / expect.rs
1 use crate::lints::{Expectation, ExpectationNote};
2 use rustc_middle::ty::query::Providers;
3 use rustc_middle::ty::TyCtxt;
4 use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
5 use rustc_session::lint::LintExpectationId;
6 use rustc_span::symbol::sym;
7 use rustc_span::Symbol;
8
9 pub(crate) fn provide(providers: &mut Providers) {
10     *providers = Providers { check_expectations, ..*providers };
11 }
12
13 fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
14     if !tcx.features().enabled(sym::lint_reasons) {
15         return;
16     }
17
18     let lint_expectations = tcx.lint_expectations(());
19     let fulfilled_expectations = tcx.sess.diagnostic().steal_fulfilled_expectation_ids();
20
21     tracing::debug!(?lint_expectations, ?fulfilled_expectations);
22
23     for (id, expectation) in lint_expectations {
24         // This check will always be true, since `lint_expectations` only
25         // holds stable ids
26         if let LintExpectationId::Stable { hir_id, .. } = id {
27             if !fulfilled_expectations.contains(&id)
28                 && tool_filter.map_or(true, |filter| expectation.lint_tool == Some(filter))
29             {
30                 let rationale = expectation.reason.map(|rationale| ExpectationNote { rationale });
31                 let note = expectation.is_unfulfilled_lint_expectations.then_some(());
32                 tcx.emit_spanned_lint(
33                     UNFULFILLED_LINT_EXPECTATIONS,
34                     *hir_id,
35                     expectation.emission_span,
36                     Expectation { rationale, note },
37                 );
38             }
39         } else {
40             unreachable!("at this stage all `LintExpectationId`s are stable");
41         }
42     }
43 }