@@ -2,8 +2,9 @@ use rustc_abi::ExternAbi;
2
2
use rustc_attr_data_structures:: { AttributeKind , ReprAttr } ;
3
3
use rustc_attr_parsing:: AttributeParser ;
4
4
use rustc_hir:: def:: { DefKind , Res } ;
5
- use rustc_hir:: intravisit:: FnKind ;
5
+ use rustc_hir:: intravisit:: { FnKind , Visitor } ;
6
6
use rustc_hir:: { AttrArgs , AttrItem , Attribute , GenericParamKind , PatExprKind , PatKind } ;
7
+ use rustc_middle:: hir:: nested_filter:: All ;
7
8
use rustc_middle:: ty;
8
9
use rustc_session:: config:: CrateType ;
9
10
use rustc_session:: { declare_lint, declare_lint_pass} ;
@@ -489,21 +490,59 @@ declare_lint! {
489
490
declare_lint_pass ! ( NonUpperCaseGlobals => [ NON_UPPER_CASE_GLOBALS ] ) ;
490
491
491
492
impl NonUpperCaseGlobals {
492
- fn check_upper_case ( cx : & LateContext < ' _ > , sort : & str , ident : & Ident ) {
493
+ fn check_upper_case ( cx : & LateContext < ' _ > , sort : & str , did : Option < LocalDefId > , ident : & Ident ) {
493
494
let name = ident. name . as_str ( ) ;
494
495
if name. chars ( ) . any ( |c| c. is_lowercase ( ) ) {
495
496
let uc = NonSnakeCase :: to_snake_case ( name) . to_uppercase ( ) ;
497
+
496
498
// We cannot provide meaningful suggestions
497
499
// if the characters are in the category of "Lowercase Letter".
498
- let sub = if * name != uc {
499
- NonUpperCaseGlobalSub :: Suggestion { span : ident. span , replace : uc }
500
+ let sub = |span| {
501
+ if * name != uc {
502
+ NonUpperCaseGlobalSub :: Suggestion { span, replace : uc. clone ( ) }
503
+ } else {
504
+ NonUpperCaseGlobalSub :: Label { span }
505
+ }
506
+ } ;
507
+
508
+ struct UsageCollector < ' a , ' tcx > {
509
+ cx : & ' tcx LateContext < ' a > ,
510
+ did : LocalDefId ,
511
+ collected : Vec < Span > ,
512
+ }
513
+
514
+ impl < ' v , ' tcx > Visitor < ' v > for UsageCollector < ' v , ' tcx > {
515
+ type NestedFilter = All ;
516
+
517
+ fn maybe_tcx ( & mut self ) -> Self :: MaybeTyCtxt {
518
+ self . cx . tcx
519
+ }
520
+
521
+ fn visit_path (
522
+ & mut self ,
523
+ path : & rustc_hir:: Path < ' v > ,
524
+ _id : rustc_hir:: HirId ,
525
+ ) -> Self :: Result {
526
+ for seg in path. segments {
527
+ if seg. res . opt_def_id ( ) == Some ( self . did . to_def_id ( ) ) {
528
+ self . collected . push ( seg. ident . span ) ;
529
+ }
530
+ }
531
+ }
532
+ }
533
+
534
+ let usages = if let Some ( did) = did {
535
+ let mut usage_collector = UsageCollector { cx, did, collected : Vec :: new ( ) } ;
536
+ cx. tcx . hir_walk_toplevel_module ( & mut usage_collector) ;
537
+ usage_collector. collected . into_iter ( ) . map ( |span| sub ( span) ) . collect ( )
500
538
} else {
501
- NonUpperCaseGlobalSub :: Label { span : ident . span }
539
+ vec ! [ ]
502
540
} ;
541
+
503
542
cx. emit_span_lint (
504
543
NON_UPPER_CASE_GLOBALS ,
505
544
ident. span ,
506
- NonUpperCaseGlobal { sort, name, sub } ,
545
+ NonUpperCaseGlobal { sort, name, sub : sub ( ident . span ) , usages } ,
507
546
) ;
508
547
}
509
548
}
@@ -516,26 +555,36 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
516
555
hir:: ItemKind :: Static ( _, ident, ..)
517
556
if !ast:: attr:: contains_name ( attrs, sym:: no_mangle) =>
518
557
{
519
- NonUpperCaseGlobals :: check_upper_case ( cx, "static variable" , & ident) ;
558
+ NonUpperCaseGlobals :: check_upper_case (
559
+ cx,
560
+ "static variable" ,
561
+ Some ( it. owner_id . def_id ) ,
562
+ & ident,
563
+ ) ;
520
564
}
521
565
hir:: ItemKind :: Const ( ident, ..) => {
522
- NonUpperCaseGlobals :: check_upper_case ( cx, "constant" , & ident) ;
566
+ NonUpperCaseGlobals :: check_upper_case (
567
+ cx,
568
+ "constant" ,
569
+ Some ( it. owner_id . def_id ) ,
570
+ & ident,
571
+ ) ;
523
572
}
524
573
_ => { }
525
574
}
526
575
}
527
576
528
577
fn check_trait_item ( & mut self , cx : & LateContext < ' _ > , ti : & hir:: TraitItem < ' _ > ) {
529
578
if let hir:: TraitItemKind :: Const ( ..) = ti. kind {
530
- NonUpperCaseGlobals :: check_upper_case ( cx, "associated constant" , & ti. ident ) ;
579
+ NonUpperCaseGlobals :: check_upper_case ( cx, "associated constant" , None , & ti. ident ) ;
531
580
}
532
581
}
533
582
534
583
fn check_impl_item ( & mut self , cx : & LateContext < ' _ > , ii : & hir:: ImplItem < ' _ > ) {
535
584
if let hir:: ImplItemKind :: Const ( ..) = ii. kind
536
585
&& !assoc_item_in_trait_impl ( cx, ii)
537
586
{
538
- NonUpperCaseGlobals :: check_upper_case ( cx, "associated constant" , & ii. ident ) ;
587
+ NonUpperCaseGlobals :: check_upper_case ( cx, "associated constant" , None , & ii. ident ) ;
539
588
}
540
589
}
541
590
@@ -551,6 +600,7 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
551
600
NonUpperCaseGlobals :: check_upper_case (
552
601
cx,
553
602
"constant in pattern" ,
603
+ None ,
554
604
& segment. ident ,
555
605
) ;
556
606
}
@@ -560,7 +610,12 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
560
610
561
611
fn check_generic_param ( & mut self , cx : & LateContext < ' _ > , param : & hir:: GenericParam < ' _ > ) {
562
612
if let GenericParamKind :: Const { .. } = param. kind {
563
- NonUpperCaseGlobals :: check_upper_case ( cx, "const parameter" , & param. name . ident ( ) ) ;
613
+ NonUpperCaseGlobals :: check_upper_case (
614
+ cx,
615
+ "const parameter" ,
616
+ Some ( param. def_id ) ,
617
+ & param. name . ident ( ) ,
618
+ ) ;
564
619
}
565
620
}
566
621
}
0 commit comments