diff options
| author | Urgau <urgau@numericable.fr> | 2025-06-21 11:39:28 +0200 |
|---|---|---|
| committer | Urgau <urgau@numericable.fr> | 2025-06-21 13:26:57 +0200 |
| commit | 8b882651330b08bb5c99be5d10d92d21422dad7c (patch) | |
| tree | 9ff0e2203b2057be28a1aeef7d54d78cfafaaf2f /compiler/rustc_lint/src | |
| parent | 09d0a739f7dc4d84ae7baeac03a7e0d94671f3cb (diff) | |
| download | rust-8b882651330b08bb5c99be5d10d92d21422dad7c.tar.gz rust-8b882651330b08bb5c99be5d10d92d21422dad7c.zip | |
Lazily collect `NonUpperCaseGlobalSubTool` diagnostics
Diffstat (limited to 'compiler/rustc_lint/src')
| -rw-r--r-- | compiler/rustc_lint/src/nonstandard_style.rs | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs index e92e8e06382..0cb3dac2482 100644 --- a/compiler/rustc_lint/src/nonstandard_style.rs +++ b/compiler/rustc_lint/src/nonstandard_style.rs @@ -1,6 +1,7 @@ use rustc_abi::ExternAbi; use rustc_attr_data_structures::{AttributeKind, ReprAttr}; use rustc_attr_parsing::AttributeParser; +use rustc_errors::LintDiagnostic; use rustc_hir::def::{DefKind, Res}; use rustc_hir::intravisit::{FnKind, Visitor}; use rustc_hir::{AttrArgs, AttrItem, Attribute, GenericParamKind, PatExprKind, PatKind}; @@ -529,25 +530,26 @@ impl NonUpperCaseGlobals { } } - let usages = if let Some(did) = did - && *name != uc - { - let mut usage_collector = UsageCollector { cx, did, collected: Vec::new() }; - cx.tcx.hir_walk_toplevel_module(&mut usage_collector); - usage_collector - .collected - .into_iter() - .map(|span| NonUpperCaseGlobalSubTool { span, replace: uc.clone() }) - .collect() - } else { - vec![] - }; + #[allow(rustc::diagnostic_outside_of_impl)] + cx.opt_span_lint(NON_UPPER_CASE_GLOBALS, ident.span.into(), |diag| { + // Compute usages lazily as it can expansive and useless when the lint is allowed. + // cf. https://github.com/rust-lang/rust/pull/142645#issuecomment-2993024625 + let usages = if let Some(did) = did + && *name != uc + { + let mut usage_collector = UsageCollector { cx, did, collected: Vec::new() }; + cx.tcx.hir_walk_toplevel_module(&mut usage_collector); + usage_collector + .collected + .into_iter() + .map(|span| NonUpperCaseGlobalSubTool { span, replace: uc.clone() }) + .collect() + } else { + vec![] + }; - cx.emit_span_lint( - NON_UPPER_CASE_GLOBALS, - ident.span, - NonUpperCaseGlobal { sort, name, sub, usages }, - ); + NonUpperCaseGlobal { sort, name, sub, usages }.decorate_lint(diag) + }); } } } |
