2024-07-04 09:55:42 +00:00
|
|
|
use rustc_data_structures::fx::FxIndexMap;
|
|
|
|
use rustc_hir::{HirId, CRATE_OWNER_ID};
|
|
|
|
use rustc_middle::lint::LintExpectation;
|
2023-05-15 04:24:45 +00:00
|
|
|
use rustc_middle::query::Providers;
|
2022-11-09 23:22:48 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
|
|
|
use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
|
2024-07-04 09:55:42 +00:00
|
|
|
use rustc_session::lint::{Level, LintExpectationId};
|
2022-03-28 22:10:45 +00:00
|
|
|
use rustc_span::Symbol;
|
2021-08-06 21:36:33 +00:00
|
|
|
|
2022-11-11 00:32:30 +00:00
|
|
|
use crate::lints::{Expectation, ExpectationNote};
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2022-03-28 22:10:45 +00:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2024-07-04 09:55:42 +00:00
|
|
|
*providers = Providers { lint_expectations, check_expectations, ..*providers };
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExpectation)> {
|
|
|
|
let krate = tcx.hir_crate_items(());
|
|
|
|
|
|
|
|
let mut expectations = Vec::new();
|
|
|
|
let mut unstable_to_stable_ids = FxIndexMap::default();
|
|
|
|
|
|
|
|
let mut record_stable = |attr_id, hir_id, attr_index| {
|
2024-07-04 10:12:39 +00:00
|
|
|
let expect_id = LintExpectationId::Stable { hir_id, attr_index, lint_index: None };
|
2024-07-04 09:55:42 +00:00
|
|
|
unstable_to_stable_ids.entry(attr_id).or_insert(expect_id);
|
|
|
|
};
|
|
|
|
let mut push_expectations = |owner| {
|
|
|
|
let lints = tcx.shallow_lint_levels_on(owner);
|
|
|
|
if lints.expectations.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
expectations.extend_from_slice(&lints.expectations);
|
|
|
|
|
|
|
|
let attrs = tcx.hir_attrs(owner);
|
|
|
|
for &(local_id, attrs) in attrs.map.iter() {
|
|
|
|
// Some attributes appear multiple times in HIR, to ensure they are correctly taken
|
|
|
|
// into account where they matter. This means we cannot just associate the AttrId to
|
|
|
|
// the first HirId where we see it, but need to check it actually appears in a lint
|
|
|
|
// level.
|
|
|
|
// FIXME(cjgillot): Can this cause an attribute to appear in multiple expectation ids?
|
|
|
|
if !lints.specs.contains_key(&local_id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (attr_index, attr) in attrs.iter().enumerate() {
|
|
|
|
let Some(Level::Expect(_)) = Level::from_attr(attr) else { continue };
|
|
|
|
record_stable(attr.id, HirId { owner, local_id }, attr_index.try_into().unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
push_expectations(CRATE_OWNER_ID);
|
|
|
|
for owner in krate.owners() {
|
|
|
|
push_expectations(owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
tcx.dcx().update_unstable_expectation_id(unstable_to_stable_ids);
|
|
|
|
expectations
|
2022-03-28 22:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
|
2022-07-22 16:48:36 +00:00
|
|
|
let lint_expectations = tcx.lint_expectations(());
|
2023-12-18 11:21:37 +00:00
|
|
|
let fulfilled_expectations = tcx.dcx().steal_fulfilled_expectation_ids();
|
2022-07-22 16:48:36 +00:00
|
|
|
|
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 {
|
2023-11-21 19:07:32 +00:00
|
|
|
if !fulfilled_expectations.contains(id)
|
2022-06-05 10:33:45 +00:00
|
|
|
&& tool_filter.map_or(true, |filter| expectation.lint_tool == Some(filter))
|
|
|
|
{
|
2022-11-11 00:32:30 +00:00
|
|
|
let rationale = expectation.reason.map(|rationale| ExpectationNote { rationale });
|
2024-08-21 04:57:58 +00:00
|
|
|
let note = expectation.is_unfulfilled_lint_expectations;
|
2024-01-16 05:27:02 +00:00
|
|
|
tcx.emit_node_span_lint(
|
2022-11-09 23:22:48 +00:00
|
|
|
UNFULFILLED_LINT_EXPECTATIONS,
|
|
|
|
*hir_id,
|
|
|
|
expectation.emission_span,
|
2022-11-11 00:32:30 +00:00
|
|
|
Expectation { rationale, note },
|
2022-11-09 23:22:48 +00:00
|
|
|
);
|
2021-11-25 16:45:11 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|