|
1 | 1 | use rustc_abi::ExternAbi; |
2 | 2 | use rustc_attr_data_structures::{AttributeKind, ReprAttr}; |
3 | 3 | use rustc_attr_parsing::AttributeParser; |
| 4 | +use rustc_errors::LintDiagnostic; |
4 | 5 | use rustc_hir::def::{DefKind, Res}; |
5 | 6 | use rustc_hir::intravisit::{FnKind, Visitor}; |
6 | 7 | use rustc_hir::{AttrArgs, AttrItem, Attribute, GenericParamKind, PatExprKind, PatKind}; |
@@ -529,25 +530,26 @@ impl NonUpperCaseGlobals { |
529 | 530 | } |
530 | 531 | } |
531 | 532 |
|
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 | + }; |
545 | 550 |
|
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 | + }); |
551 | 553 | } |
552 | 554 | } |
553 | 555 | } |
|
0 commit comments