1
Fork 0

non_local_defs: improve some notes around trait, bounds, consts

- Restrict const-anon exception diag to relevant places
 - Invoke bounds (and type-inference) in non_local_defs
 - Specialize diagnostic for impl without Trait
This commit is contained in:
Urgau 2024-05-13 20:15:57 +02:00
parent 06c6a2d9d6
commit de1c122950
12 changed files with 141 additions and 128 deletions

View file

@ -1335,7 +1335,8 @@ pub enum NonLocalDefinitionsDiag {
body_kind_descr: &'static str,
body_name: String,
cargo_update: Option<NonLocalDefinitionsCargoUpdateNote>,
const_anon: Option<Span>,
const_anon: Option<Option<Span>>,
has_trait: bool,
},
MacroRules {
depth: u32,
@ -1356,6 +1357,7 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
body_name,
cargo_update,
const_anon,
has_trait,
} => {
diag.primary_message(fluent::lint_non_local_definitions_impl);
diag.arg("depth", depth);
@ -1363,21 +1365,29 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
diag.arg("body_name", body_name);
diag.help(fluent::lint_help);
diag.note(fluent::lint_non_local);
diag.note(fluent::lint_exception);
diag.note(fluent::lint_non_local_definitions_deprecation);
if has_trait {
diag.note(fluent::lint_bounds);
diag.note(fluent::lint_with_trait);
} else {
diag.note(fluent::lint_without_trait);
}
if let Some(cargo_update) = cargo_update {
diag.subdiagnostic(&diag.dcx, cargo_update);
}
if let Some(const_anon) = const_anon {
diag.span_suggestion(
const_anon,
fluent::lint_const_anon,
"_",
Applicability::MachineApplicable,
);
diag.note(fluent::lint_exception);
if let Some(const_anon) = const_anon {
diag.span_suggestion(
const_anon,
fluent::lint_const_anon,
"_",
Applicability::MachineApplicable,
);
}
}
diag.note(fluent::lint_non_local_definitions_deprecation);
}
NonLocalDefinitionsDiag::MacroRules {
depth,

View file

@ -202,8 +202,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
// Get the span of the parent const item ident (if it's a not a const anon).
//
// Used to suggest changing the const item to a const anon.
let span_for_const_anon_suggestion = if self.body_depth == 1
&& parent_def_kind == DefKind::Const
let span_for_const_anon_suggestion = if parent_def_kind == DefKind::Const
&& parent_opt_item_name != Some(kw::Underscore)
&& let Some(parent) = parent.as_local()
&& let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent)
@ -215,6 +214,9 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
None
};
let const_anon = matches!(parent_def_kind, DefKind::Const | DefKind::Static { .. })
.then_some(span_for_const_anon_suggestion);
cx.emit_span_lint(
NON_LOCAL_DEFINITIONS,
item.span,
@ -225,7 +227,8 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
.map(|s| s.to_ident_string())
.unwrap_or_else(|| "<unnameable>".to_string()),
cargo_update: cargo_update(),
const_anon: span_for_const_anon_suggestion,
const_anon,
has_trait: impl_.of_trait.is_some(),
},
)
}