]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/expect.rs
Rollup merge of #98126 - fortanix:raoul/mitigate_stale_data_vulnerability, r=cuviper
[rust.git] / compiler / rustc_lint / src / expect.rs
1 use crate::builtin;
2 use rustc_hir::HirId;
3 use rustc_middle::ty::query::Providers;
4 use rustc_middle::{lint::LintExpectation, ty::TyCtxt};
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.sess.features_untracked().enabled(sym::lint_reasons) {
15         return;
16     }
17
18     let fulfilled_expectations = tcx.sess.diagnostic().steal_fulfilled_expectation_ids();
19     let lint_expectations = &tcx.lint_levels(()).lint_expectations;
20
21     for (id, expectation) in lint_expectations {
22         // This check will always be true, since `lint_expectations` only
23         // holds stable ids
24         if let LintExpectationId::Stable { hir_id, .. } = id {
25             if !fulfilled_expectations.contains(&id)
26                 && tool_filter.map_or(true, |filter| expectation.lint_tool == Some(filter))
27             {
28                 emit_unfulfilled_expectation_lint(tcx, *hir_id, expectation);
29             }
30         } else {
31             unreachable!("at this stage all `LintExpectationId`s are stable");
32         }
33     }
34 }
35
36 fn emit_unfulfilled_expectation_lint(
37     tcx: TyCtxt<'_>,
38     hir_id: HirId,
39     expectation: &LintExpectation,
40 ) {
41     tcx.struct_span_lint_hir(
42         builtin::UNFULFILLED_LINT_EXPECTATIONS,
43         hir_id,
44         expectation.emission_span,
45         |diag| {
46             let mut diag = diag.build("this lint expectation is unfulfilled");
47             if let Some(rationale) = expectation.reason {
48                 diag.note(rationale.as_str());
49             }
50
51             if expectation.is_unfulfilled_lint_expectations {
52                 diag.note("the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message");
53             }
54
55             diag.emit();
56         },
57     );
58 }