]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/expect.rs
Auto merge of #100966 - compiler-errors:revert-remove-deferred-sized-checks, r=pnkfelix
[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 fulfilled_expectations = tcx.sess.diagnostic().steal_fulfilled_expectation_ids();
20     let lint_expectations = &tcx.lint_levels(()).lint_expectations;
21
22     for (id, expectation) in lint_expectations {
23         // This check will always be true, since `lint_expectations` only
24         // holds stable ids
25         if let LintExpectationId::Stable { hir_id, .. } = id {
26             if !fulfilled_expectations.contains(&id)
27                 && tool_filter.map_or(true, |filter| expectation.lint_tool == Some(filter))
28             {
29                 emit_unfulfilled_expectation_lint(tcx, *hir_id, expectation);
30             }
31         } else {
32             unreachable!("at this stage all `LintExpectationId`s are stable");
33         }
34     }
35 }
36
37 fn emit_unfulfilled_expectation_lint(
38     tcx: TyCtxt<'_>,
39     hir_id: HirId,
40     expectation: &LintExpectation,
41 ) {
42     tcx.struct_span_lint_hir(
43         builtin::UNFULFILLED_LINT_EXPECTATIONS,
44         hir_id,
45         expectation.emission_span,
46         |diag| {
47             let mut diag = diag.build(fluent::lint::expectation);
48             if let Some(rationale) = expectation.reason {
49                 diag.note(rationale.as_str());
50             }
51
52             if expectation.is_unfulfilled_lint_expectations {
53                 diag.note(fluent::lint::note);
54             }
55
56             diag.emit();
57         },
58     );
59 }