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