1
Fork 0

Improve check-cfg Cargo macro diagnostic with crate name

This commit is contained in:
Urgau 2024-12-15 12:52:04 +01:00
parent e3e5bd95cd
commit 291c519c69
5 changed files with 19 additions and 14 deletions

View file

@ -21,7 +21,7 @@ mod check_cfg;
pub(super) fn decorate_lint(
sess: &Session,
_tcx: Option<TyCtxt<'_>>,
tcx: Option<TyCtxt<'_>>,
diagnostic: BuiltinLintDiag,
diag: &mut Diag<'_, ()>,
) {
@ -205,10 +205,10 @@ pub(super) fn decorate_lint(
.decorate_lint(diag);
}
BuiltinLintDiag::UnexpectedCfgName(name, value) => {
check_cfg::unexpected_cfg_name(sess, name, value).decorate_lint(diag);
check_cfg::unexpected_cfg_name(sess, tcx, name, value).decorate_lint(diag);
}
BuiltinLintDiag::UnexpectedCfgValue(name, value) => {
check_cfg::unexpected_cfg_value(sess, name, value).decorate_lint(diag);
check_cfg::unexpected_cfg_value(sess, tcx, name, value).decorate_lint(diag);
}
BuiltinLintDiag::DeprecatedWhereclauseLocation(left_sp, sugg) => {
let suggestion = match sugg {

View file

@ -1,5 +1,6 @@
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::bug;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_session::config::ExpectedValues;
use rustc_span::edit_distance::find_best_match_for_name;
@ -73,17 +74,20 @@ fn rustc_macro_help(span: Span) -> Option<lints::UnexpectedCfgRustcMacroHelp> {
}
}
fn cargo_macro_help(span: Span) -> Option<lints::UnexpectedCfgCargoMacroHelp> {
fn cargo_macro_help(
tcx: Option<TyCtxt<'_>>,
span: Span,
) -> Option<lints::UnexpectedCfgCargoMacroHelp> {
let oexpn = span.ctxt().outer_expn_data();
if let Some(def_id) = oexpn.macro_def_id
&& let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind
&& def_id.krate != LOCAL_CRATE
&& let Some(tcx) = tcx
{
Some(lints::UnexpectedCfgCargoMacroHelp {
macro_kind: macro_kind.descr(),
macro_name,
// FIXME: Get access to a `TyCtxt` from an `EarlyContext`
// crate_name: cx.tcx.crate_name(def_id.krate),
crate_name: tcx.crate_name(def_id.krate),
})
} else {
None
@ -92,6 +96,7 @@ fn cargo_macro_help(span: Span) -> Option<lints::UnexpectedCfgCargoMacroHelp> {
pub(super) fn unexpected_cfg_name(
sess: &Session,
tcx: Option<TyCtxt<'_>>,
(name, name_span): (Symbol, Span),
value: Option<(Symbol, Span)>,
) -> lints::UnexpectedCfgName {
@ -223,7 +228,7 @@ pub(super) fn unexpected_cfg_name(
};
lints::unexpected_cfg_name::InvocationHelp::Cargo {
help,
macro_help: cargo_macro_help(name_span),
macro_help: cargo_macro_help(tcx, name_span),
}
} else {
let help = lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No));
@ -238,6 +243,7 @@ pub(super) fn unexpected_cfg_name(
pub(super) fn unexpected_cfg_value(
sess: &Session,
tcx: Option<TyCtxt<'_>>,
(name, name_span): (Symbol, Span),
value: Option<(Symbol, Span)>,
) -> lints::UnexpectedCfgValue {
@ -339,7 +345,7 @@ pub(super) fn unexpected_cfg_value(
};
lints::unexpected_cfg_value::InvocationHelp::Cargo {
help,
macro_help: cargo_macro_help(name_span),
macro_help: cargo_macro_help(tcx, name_span),
}
} else {
let help = if can_suggest_adding_value {

View file

@ -2187,8 +2187,7 @@ pub(crate) struct UnexpectedCfgRustcMacroHelp {
pub(crate) struct UnexpectedCfgCargoMacroHelp {
pub macro_kind: &'static str,
pub macro_name: Symbol,
// FIXME: Figure out a way to get the crate name
// crate_name: String,
pub crate_name: Symbol,
}
#[derive(LintDiagnostic)]