Skip to content

Commit 1b5ec3f

Browse files
committed
Add emit_span_lint_lazy to lazily create LintDiagnostic structs
1 parent 8b88265 commit 1b5ec3f

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

compiler/rustc_lint/src/context.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,20 @@ pub trait LintContext {
524524
});
525525
}
526526

527+
/// Emit a lint at `span` from a lazily-constructed lint struct (some type that implements
528+
/// `LintDiagnostic`, typically generated by `#[derive(LintDiagnostic)]`).
529+
fn emit_span_lint_lazy<S: Into<MultiSpan>, L: for<'a> LintDiagnostic<'a, ()>>(
530+
&self,
531+
lint: &'static Lint,
532+
span: S,
533+
decorator: impl FnOnce() -> L,
534+
) {
535+
self.opt_span_lint(lint, Some(span), |lint| {
536+
let decorator = decorator();
537+
decorator.decorate_lint(lint);
538+
});
539+
}
540+
527541
/// Emit a lint at the appropriate level, with an associated span.
528542
///
529543
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc_abi::ExternAbi;
22
use rustc_attr_data_structures::{AttributeKind, ReprAttr};
33
use rustc_attr_parsing::AttributeParser;
4-
use rustc_errors::LintDiagnostic;
54
use rustc_hir::def::{DefKind, Res};
65
use rustc_hir::intravisit::{FnKind, Visitor};
76
use rustc_hir::{AttrArgs, AttrItem, Attribute, GenericParamKind, PatExprKind, PatKind};
@@ -530,8 +529,7 @@ impl NonUpperCaseGlobals {
530529
}
531530
}
532531

533-
#[allow(rustc::diagnostic_outside_of_impl)]
534-
cx.opt_span_lint(NON_UPPER_CASE_GLOBALS, ident.span.into(), |diag| {
532+
cx.emit_span_lint_lazy(NON_UPPER_CASE_GLOBALS, ident.span, || {
535533
// Compute usages lazily as it can expansive and useless when the lint is allowed.
536534
// cf. https://github.com/rust-lang/rust/pull/142645#issuecomment-2993024625
537535
let usages = if let Some(did) = did
@@ -548,7 +546,7 @@ impl NonUpperCaseGlobals {
548546
vec![]
549547
};
550548

551-
NonUpperCaseGlobal { sort, name, sub, usages }.decorate_lint(diag)
549+
NonUpperCaseGlobal { sort, name, sub, usages }
552550
});
553551
}
554552
}

0 commit comments

Comments
 (0)