2021-08-06 21:36:33 +00:00
|
|
|
use crate::builtin;
|
2022-06-27 13:15:31 +00:00
|
|
|
use rustc_errors::fluent;
|
2021-11-25 16:45:11 +00:00
|
|
|
use rustc_hir::HirId;
|
2022-03-28 22:10:45 +00:00
|
|
|
use rustc_middle::ty::query::Providers;
|
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;
|
2022-03-28 22:10:45 +00:00
|
|
|
use rustc_span::Symbol;
|
2021-08-06 21:36:33 +00:00
|
|
|
|
2022-03-28 22:10:45 +00:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
|
|
|
*providers = Providers { check_expectations, ..*providers };
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
|
2021-08-06 21:36:33 +00:00
|
|
|
if !tcx.sess.features_untracked().enabled(sym::lint_reasons) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let fulfilled_expectations = tcx.sess.diagnostic().steal_fulfilled_expectation_ids();
|
2022-09-20 18:04:35 +00:00
|
|
|
let lint_expectations = &tcx.lint_levels(()).lint_expectations;
|
2021-08-06 21:36:33 +00:00
|
|
|
|
|
|
|
for (id, expectation) in lint_expectations {
|
2022-06-05 10:33:45 +00:00
|
|
|
// This check will always be true, since `lint_expectations` only
|
|
|
|
// holds stable ids
|
|
|
|
if let LintExpectationId::Stable { hir_id, .. } = id {
|
|
|
|
if !fulfilled_expectations.contains(&id)
|
|
|
|
&& tool_filter.map_or(true, |filter| expectation.lint_tool == Some(filter))
|
|
|
|
{
|
2021-11-25 16:45:11 +00:00
|
|
|
emit_unfulfilled_expectation_lint(tcx, *hir_id, expectation);
|
|
|
|
}
|
2022-06-05 10:33:45 +00:00
|
|
|
} else {
|
|
|
|
unreachable!("at this stage all `LintExpectationId`s are stable");
|
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,
|
2022-09-16 07:01:02 +00:00
|
|
|
fluent::lint::expectation,
|
|
|
|
|lint| {
|
2021-08-06 21:36:33 +00:00
|
|
|
if let Some(rationale) = expectation.reason {
|
2022-09-16 07:01:02 +00:00
|
|
|
lint.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 {
|
2022-09-16 07:01:02 +00:00
|
|
|
lint.note(fluent::lint::note);
|
2022-03-06 13:18:28 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 07:01:02 +00:00
|
|
|
lint
|
2021-08-06 21:36:33 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|