Add emit_span_lint_lazy to lazily create LintDiagnostic structs

This commit is contained in:
Urgau
2025-06-21 13:40:05 +02:00
parent 8b88265133
commit 1b5ec3fa1d
2 changed files with 16 additions and 4 deletions

View File

@@ -524,6 +524,20 @@ pub trait LintContext {
});
}
/// Emit a lint at `span` from a lazily-constructed lint struct (some type that implements
/// `LintDiagnostic`, typically generated by `#[derive(LintDiagnostic)]`).
fn emit_span_lint_lazy<S: Into<MultiSpan>, L: for<'a> LintDiagnostic<'a, ()>>(
&self,
lint: &'static Lint,
span: S,
decorator: impl FnOnce() -> L,
) {
self.opt_span_lint(lint, Some(span), |lint| {
let decorator = decorator();
decorator.decorate_lint(lint);
});
}
/// Emit a lint at the appropriate level, with an associated span.
///
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature