]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/expect.rs
cf8f31bcbd0650780b9374bed416b0cb9c9f95ce
[rust.git] / compiler / rustc_lint / src / expect.rs
1 use crate::builtin;
2 use rustc_errors::fluent;
3 use rustc_hir::HirId;
4 use rustc_middle::ty::query::Providers;
5 use rustc_middle::{lint::LintExpectation, ty::TyCtxt};
6 use rustc_session::lint::LintExpectationId;
7 use rustc_span::symbol::sym;
8 use rustc_span::Symbol;
9
10 pub(crate) fn provide(providers: &mut Providers) {
11     *providers = Providers { check_expectations, ..*providers };
12 }
13
14 fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
15     if !tcx.sess.features_untracked().enabled(sym::lint_reasons) {
16         return;
17     }
18
19     let lint_expectations = tcx.lint_expectations(());
20     let fulfilled_expectations = tcx.sess.diagnostic().steal_fulfilled_expectation_ids();
21
22     tracing::debug!(?lint_expectations, ?fulfilled_expectations);
23
24     for (id, expectation) in lint_expectations {
25         // This check will always be true, since `lint_expectations` only
26         // holds stable ids
27         if let LintExpectationId::Stable { hir_id, .. } = id {
28             if !fulfilled_expectations.contains(&id)
29                 && tool_filter.map_or(true, |filter| expectation.lint_tool == Some(filter))
30             {
31                 emit_unfulfilled_expectation_lint(tcx, *hir_id, expectation);
32             }
33         } else {
34             unreachable!("at this stage all `LintExpectationId`s are stable");
35         }
36     }
37 }
38
39 fn emit_unfulfilled_expectation_lint(
40     tcx: TyCtxt<'_>,
41     hir_id: HirId,
42     expectation: &LintExpectation,
43 ) {
44     tcx.struct_span_lint_hir(
45         builtin::UNFULFILLED_LINT_EXPECTATIONS,
46         hir_id,
47         expectation.emission_span,
48         fluent::lint_expectation,
49         |lint| {
50             if let Some(rationale) = expectation.reason {
51                 lint.note(rationale.as_str());
52             }
53
54             if expectation.is_unfulfilled_lint_expectations {
55                 lint.note(fluent::note);
56             }
57
58             lint
59         },
60     );
61 }