]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/expect.rs
49664322a98bf3125a61f68d3a79b19c72ab8944
[rust.git] / compiler / rustc_lint / src / expect.rs
1 use crate::builtin;
2 use rustc_data_structures::fx::FxHashMap;
3 use rustc_middle::lint::struct_lint_level;
4 use rustc_middle::{lint::LintExpectation, ty::TyCtxt};
5 use rustc_session::lint::LintExpectationId;
6 use rustc_span::symbol::sym;
7 use rustc_span::MultiSpan;
8
9 pub fn check_expectations(tcx: TyCtxt<'_>) {
10     if !tcx.sess.features_untracked().enabled(sym::lint_reasons) {
11         return;
12     }
13
14     let fulfilled_expectations = tcx.sess.diagnostic().steal_fulfilled_expectation_ids();
15     let lint_expectations: &FxHashMap<LintExpectationId, LintExpectation> =
16         &tcx.lint_levels(()).lint_expectations;
17
18     for (id, expectation) in lint_expectations {
19         if fulfilled_expectations.contains(id) {
20             continue;
21         }
22
23         emit_unfulfilled_expectation_lint(tcx, expectation);
24     }
25 }
26
27 fn emit_unfulfilled_expectation_lint(tcx: TyCtxt<'_>, expectation: &LintExpectation) {
28     // FIXME: The current implementation doesn't cover cases where the
29     // `unfulfilled_lint_expectations` is actually expected by another lint
30     // expectation. This can be added here as we have the lint level of this
31     // expectation, and we can also mark the lint expectation it would fulfill
32     // as such. This is currently not implemented to get some early feedback
33     // before diving deeper into this.
34     struct_lint_level(
35         tcx.sess,
36         builtin::UNFULFILLED_LINT_EXPECTATIONS,
37         expectation.emission_level,
38         expectation.emission_level_source,
39         Some(MultiSpan::from_span(expectation.emission_span)),
40         |diag| {
41             let mut diag = diag.build("this lint expectation is unfulfilled");
42             if let Some(rationale) = expectation.reason {
43                 diag.note(&rationale.as_str());
44             }
45             diag.emit();
46         },
47     );
48 }