2021-08-06 21:36:33 +00:00
|
|
|
use crate::builtin;
|
2021-11-25 16:45:11 +00:00
|
|
|
use rustc_hir::HirId;
|
2021-08-06 21:36:33 +00:00
|
|
|
use rustc_middle::{lint::LintExpectation, ty::TyCtxt};
|
|
|
|
use rustc_session::lint::LintExpectationId;
|
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
|
|
|
|
pub fn check_expectations(tcx: TyCtxt<'_>) {
|
|
|
|
if !tcx.sess.features_untracked().enabled(sym::lint_reasons) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let fulfilled_expectations = tcx.sess.diagnostic().steal_fulfilled_expectation_ids();
|
2022-03-02 17:10:07 +00:00
|
|
|
let lint_expectations = &tcx.lint_levels(()).lint_expectations;
|
2021-08-06 21:36:33 +00:00
|
|
|
|
|
|
|
for (id, expectation) in lint_expectations {
|
2021-08-06 21:38:09 +00:00
|
|
|
if !fulfilled_expectations.contains(id) {
|
2021-11-25 16:45:11 +00:00
|
|
|
// This check will always be true, since `lint_expectations` only
|
|
|
|
// holds stable ids
|
|
|
|
if let LintExpectationId::Stable { hir_id, .. } = id {
|
|
|
|
emit_unfulfilled_expectation_lint(tcx, *hir_id, expectation);
|
2021-11-25 20:59:55 +00:00
|
|
|
} else {
|
|
|
|
unreachable!("at this stage all `LintExpectationId`s are stable");
|
2021-11-25 16:45:11 +00:00
|
|
|
}
|
2021-08-06 21:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 16:45:11 +00:00
|
|
|
fn emit_unfulfilled_expectation_lint(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
hir_id: HirId,
|
|
|
|
expectation: &LintExpectation,
|
|
|
|
) {
|
|
|
|
tcx.struct_span_lint_hir(
|
2021-08-06 21:36:33 +00:00
|
|
|
builtin::UNFULFILLED_LINT_EXPECTATIONS,
|
2021-11-25 16:45:11 +00:00
|
|
|
hir_id,
|
|
|
|
expectation.emission_span,
|
2021-08-06 21:36:33 +00:00
|
|
|
|diag| {
|
|
|
|
let mut diag = diag.build("this lint expectation is unfulfilled");
|
|
|
|
if let Some(rationale) = expectation.reason {
|
2022-03-26 07:27:43 +00:00
|
|
|
diag.note(rationale.as_str());
|
2021-08-06 21:36:33 +00:00
|
|
|
}
|
2022-03-06 13:18:28 +00:00
|
|
|
|
|
|
|
if expectation.is_unfulfilled_lint_expectations {
|
|
|
|
diag.note("the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message");
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:36:33 +00:00
|
|
|
diag.emit();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|