Skip to content

Commit 8b88265

Browse files
committed
Lazily collect NonUpperCaseGlobalSubTool diagnostics
1 parent 09d0a73 commit 8b88265

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_abi::ExternAbi;
22
use rustc_attr_data_structures::{AttributeKind, ReprAttr};
33
use rustc_attr_parsing::AttributeParser;
4+
use rustc_errors::LintDiagnostic;
45
use rustc_hir::def::{DefKind, Res};
56
use rustc_hir::intravisit::{FnKind, Visitor};
67
use rustc_hir::{AttrArgs, AttrItem, Attribute, GenericParamKind, PatExprKind, PatKind};
@@ -529,25 +530,26 @@ impl NonUpperCaseGlobals {
529530
}
530531
}
531532

532-
let usages = if let Some(did) = did
533-
&& *name != uc
534-
{
535-
let mut usage_collector = UsageCollector { cx, did, collected: Vec::new() };
536-
cx.tcx.hir_walk_toplevel_module(&mut usage_collector);
537-
usage_collector
538-
.collected
539-
.into_iter()
540-
.map(|span| NonUpperCaseGlobalSubTool { span, replace: uc.clone() })
541-
.collect()
542-
} else {
543-
vec![]
544-
};
533+
#[allow(rustc::diagnostic_outside_of_impl)]
534+
cx.opt_span_lint(NON_UPPER_CASE_GLOBALS, ident.span.into(), |diag| {
535+
// Compute usages lazily as it can expansive and useless when the lint is allowed.
536+
// cf. https://github.com/rust-lang/rust/pull/142645#issuecomment-2993024625
537+
let usages = if let Some(did) = did
538+
&& *name != uc
539+
{
540+
let mut usage_collector = UsageCollector { cx, did, collected: Vec::new() };
541+
cx.tcx.hir_walk_toplevel_module(&mut usage_collector);
542+
usage_collector
543+
.collected
544+
.into_iter()
545+
.map(|span| NonUpperCaseGlobalSubTool { span, replace: uc.clone() })
546+
.collect()
547+
} else {
548+
vec![]
549+
};
545550

546-
cx.emit_span_lint(
547-
NON_UPPER_CASE_GLOBALS,
548-
ident.span,
549-
NonUpperCaseGlobal { sort, name, sub, usages },
550-
);
551+
NonUpperCaseGlobal { sort, name, sub, usages }.decorate_lint(diag)
552+
});
551553
}
552554
}
553555
}

0 commit comments

Comments
 (0)