]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/expect.rs
Handle `#[expect(unfulfilled_lint_expectations)]` with a lint message
[rust.git] / compiler / rustc_lint / src / expect.rs
1 use crate::builtin;
2 use rustc_hir::HirId;
3 use rustc_middle::{lint::LintExpectation, ty::TyCtxt};
4 use rustc_session::lint::LintExpectationId;
5 use rustc_span::symbol::sym;
6
7 pub fn check_expectations(tcx: TyCtxt<'_>) {
8     if !tcx.sess.features_untracked().enabled(sym::lint_reasons) {
9         return;
10     }
11
12     let fulfilled_expectations = tcx.sess.diagnostic().steal_fulfilled_expectation_ids();
13     let lint_expectations = &tcx.lint_levels(()).lint_expectations;
14
15     for (id, expectation) in lint_expectations {
16         if !fulfilled_expectations.contains(id) {
17             // This check will always be true, since `lint_expectations` only
18             // holds stable ids
19             if let LintExpectationId::Stable { hir_id, .. } = id {
20                 emit_unfulfilled_expectation_lint(tcx, *hir_id, expectation);
21             } else {
22                 unreachable!("at this stage all `LintExpectationId`s are stable");
23             }
24         }
25     }
26 }
27
28 fn emit_unfulfilled_expectation_lint(
29     tcx: TyCtxt<'_>,
30     hir_id: HirId,
31     expectation: &LintExpectation,
32 ) {
33     tcx.struct_span_lint_hir(
34         builtin::UNFULFILLED_LINT_EXPECTATIONS,
35         hir_id,
36         expectation.emission_span,
37         |diag| {
38             let mut diag = diag.build("this lint expectation is unfulfilled");
39             if let Some(rationale) = expectation.reason {
40                 diag.note(&rationale.as_str());
41             }
42
43             if expectation.is_unfulfilled_lint_expectations {
44                 diag.note("the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message");
45             }
46
47             diag.emit();
48         },
49     );
50 }