@@ -14,13 +14,12 @@ mod case_conv;
1414
1515use std:: fmt;
1616
17- use base_db:: CrateId ;
1817use hir_def:: {
1918 data:: adt:: VariantData ,
2019 hir:: { Pat , PatId } ,
2120 src:: HasSource ,
22- AdtId , AttrDefId , ConstId , EnumId , FunctionId , ItemContainerId , Lookup , ModuleDefId , StaticId ,
23- StructId ,
21+ AdtId , AttrDefId , ConstId , DefWithBodyId , EnumId , EnumVariantId , FunctionId , ItemContainerId ,
22+ Lookup , ModuleDefId , StaticId , StructId ,
2423} ;
2524use hir_expand:: {
2625 name:: { AsName , Name } ,
@@ -44,13 +43,9 @@ mod allow {
4443 pub ( super ) const NON_CAMEL_CASE_TYPES : & str = "non_camel_case_types" ;
4544}
4645
47- pub fn incorrect_case (
48- db : & dyn HirDatabase ,
49- krate : CrateId ,
50- owner : ModuleDefId ,
51- ) -> Vec < IncorrectCase > {
46+ pub fn incorrect_case ( db : & dyn HirDatabase , owner : ModuleDefId ) -> Vec < IncorrectCase > {
5247 let _p = profile:: span ( "validate_module_item" ) ;
53- let mut validator = DeclValidator :: new ( db, krate ) ;
48+ let mut validator = DeclValidator :: new ( db) ;
5449 validator. validate_item ( owner) ;
5550 validator. sink
5651}
@@ -120,7 +115,6 @@ pub struct IncorrectCase {
120115
121116pub ( super ) struct DeclValidator < ' a > {
122117 db : & ' a dyn HirDatabase ,
123- krate : CrateId ,
124118 pub ( super ) sink : Vec < IncorrectCase > ,
125119}
126120
@@ -132,8 +126,8 @@ struct Replacement {
132126}
133127
134128impl < ' a > DeclValidator < ' a > {
135- pub ( super ) fn new ( db : & ' a dyn HirDatabase , krate : CrateId ) -> DeclValidator < ' a > {
136- DeclValidator { db, krate , sink : Vec :: new ( ) }
129+ pub ( super ) fn new ( db : & ' a dyn HirDatabase ) -> DeclValidator < ' a > {
130+ DeclValidator { db, sink : Vec :: new ( ) }
137131 }
138132
139133 pub ( super ) fn validate_item ( & mut self , item : ModuleDefId ) {
@@ -195,8 +189,7 @@ impl<'a> DeclValidator<'a> {
195189 AttrDefId :: TypeAliasId ( _) => None ,
196190 AttrDefId :: GenericParamId ( _) => None ,
197191 }
198- . map ( |mid| self . allowed ( mid, allow_name, true ) )
199- . unwrap_or ( false )
192+ . is_some_and ( |mid| self . allowed ( mid, allow_name, true ) )
200193 }
201194
202195 fn validate_func ( & mut self , func : FunctionId ) {
@@ -206,17 +199,7 @@ impl<'a> DeclValidator<'a> {
206199 return ;
207200 }
208201
209- let body = self . db . body ( func. into ( ) ) ;
210-
211- // Recursively validate inner scope items, such as static variables and constants.
212- for ( _, block_def_map) in body. blocks ( self . db . upcast ( ) ) {
213- for ( _, module) in block_def_map. modules ( ) {
214- for def_id in module. scope . declarations ( ) {
215- let mut validator = DeclValidator :: new ( self . db , self . krate ) ;
216- validator. validate_item ( def_id) ;
217- }
218- }
219- }
202+ self . validate_body_inner_items ( func. into ( ) ) ;
220203
221204 // Check whether non-snake case identifiers are allowed for this function.
222205 if self . allowed ( func. into ( ) , allow:: NON_SNAKE_CASE , false ) {
@@ -231,6 +214,8 @@ impl<'a> DeclValidator<'a> {
231214 expected_case : CaseType :: LowerSnakeCase ,
232215 } ) ;
233216
217+ let body = self . db . body ( func. into ( ) ) ;
218+
234219 // Check the patterns inside the function body.
235220 // This includes function parameters.
236221 let pats_replacements = body
@@ -496,6 +481,11 @@ impl<'a> DeclValidator<'a> {
496481 fn validate_enum ( & mut self , enum_id : EnumId ) {
497482 let data = self . db . enum_data ( enum_id) ;
498483
484+ for ( local_id, _) in data. variants . iter ( ) {
485+ let variant_id = EnumVariantId { parent : enum_id, local_id } ;
486+ self . validate_body_inner_items ( variant_id. into ( ) ) ;
487+ }
488+
499489 // Check whether non-camel case names are allowed for this enum.
500490 if self . allowed ( enum_id. into ( ) , allow:: NON_CAMEL_CASE_TYPES , false ) {
501491 return ;
@@ -512,13 +502,11 @@ impl<'a> DeclValidator<'a> {
512502 // Check the field names.
513503 let enum_fields_replacements = data
514504 . variants
515- . iter ( )
516- . filter_map ( |( _ , variant) | {
505+ . values ( )
506+ . filter_map ( |variant| {
517507 Some ( Replacement {
518508 current_name : variant. name . clone ( ) ,
519- suggested_text : to_camel_case (
520- & variant. name . display ( self . db . upcast ( ) ) . to_string ( ) ,
521- ) ?,
509+ suggested_text : to_camel_case ( & variant. name . to_smol_str ( ) ) ?,
522510 expected_case : CaseType :: UpperCamelCase ,
523511 } )
524512 } )
@@ -622,6 +610,8 @@ impl<'a> DeclValidator<'a> {
622610 fn validate_const ( & mut self , const_id : ConstId ) {
623611 let data = self . db . const_data ( const_id) ;
624612
613+ self . validate_body_inner_items ( const_id. into ( ) ) ;
614+
625615 if self . allowed ( const_id. into ( ) , allow:: NON_UPPER_CASE_GLOBAL , false ) {
626616 return ;
627617 }
@@ -631,7 +621,7 @@ impl<'a> DeclValidator<'a> {
631621 None => return ,
632622 } ;
633623
634- let const_name = name. display ( self . db . upcast ( ) ) . to_string ( ) ;
624+ let const_name = name. to_smol_str ( ) ;
635625 let replacement = if let Some ( new_name) = to_upper_snake_case ( & const_name) {
636626 Replacement {
637627 current_name : name. clone ( ) ,
@@ -670,13 +660,15 @@ impl<'a> DeclValidator<'a> {
670660 return ;
671661 }
672662
663+ self . validate_body_inner_items ( static_id. into ( ) ) ;
664+
673665 if self . allowed ( static_id. into ( ) , allow:: NON_UPPER_CASE_GLOBAL , false ) {
674666 return ;
675667 }
676668
677669 let name = & data. name ;
678670
679- let static_name = name. display ( self . db . upcast ( ) ) . to_string ( ) ;
671+ let static_name = name. to_smol_str ( ) ;
680672 let replacement = if let Some ( new_name) = to_upper_snake_case ( & static_name) {
681673 Replacement {
682674 current_name : name. clone ( ) ,
@@ -707,4 +699,17 @@ impl<'a> DeclValidator<'a> {
707699
708700 self . sink . push ( diagnostic) ;
709701 }
702+
703+ // FIXME: We don't currently validate names within `DefWithBodyId::InTypeConstId`.
704+ /// Recursively validates inner scope items, such as static variables and constants.
705+ fn validate_body_inner_items ( & mut self , body_id : DefWithBodyId ) {
706+ let body = self . db . body ( body_id) ;
707+ for ( _, block_def_map) in body. blocks ( self . db . upcast ( ) ) {
708+ for ( _, module) in block_def_map. modules ( ) {
709+ for def_id in module. scope . declarations ( ) {
710+ self . validate_item ( def_id) ;
711+ }
712+ }
713+ }
714+ }
710715}
0 commit comments