@@ -14,13 +14,12 @@ mod case_conv;
14
14
15
15
use std:: fmt;
16
16
17
- use base_db:: CrateId ;
18
17
use hir_def:: {
19
18
data:: adt:: VariantData ,
20
19
hir:: { Pat , PatId } ,
21
20
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 ,
24
23
} ;
25
24
use hir_expand:: {
26
25
name:: { AsName , Name } ,
@@ -44,13 +43,9 @@ mod allow {
44
43
pub ( super ) const NON_CAMEL_CASE_TYPES : & str = "non_camel_case_types" ;
45
44
}
46
45
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 > {
52
47
let _p = profile:: span ( "validate_module_item" ) ;
53
- let mut validator = DeclValidator :: new ( db, krate ) ;
48
+ let mut validator = DeclValidator :: new ( db) ;
54
49
validator. validate_item ( owner) ;
55
50
validator. sink
56
51
}
@@ -120,7 +115,6 @@ pub struct IncorrectCase {
120
115
121
116
pub ( super ) struct DeclValidator < ' a > {
122
117
db : & ' a dyn HirDatabase ,
123
- krate : CrateId ,
124
118
pub ( super ) sink : Vec < IncorrectCase > ,
125
119
}
126
120
@@ -132,8 +126,8 @@ struct Replacement {
132
126
}
133
127
134
128
impl < ' 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 ( ) }
137
131
}
138
132
139
133
pub ( super ) fn validate_item ( & mut self , item : ModuleDefId ) {
@@ -195,8 +189,7 @@ impl<'a> DeclValidator<'a> {
195
189
AttrDefId :: TypeAliasId ( _) => None ,
196
190
AttrDefId :: GenericParamId ( _) => None ,
197
191
}
198
- . map ( |mid| self . allowed ( mid, allow_name, true ) )
199
- . unwrap_or ( false )
192
+ . is_some_and ( |mid| self . allowed ( mid, allow_name, true ) )
200
193
}
201
194
202
195
fn validate_func ( & mut self , func : FunctionId ) {
@@ -206,17 +199,7 @@ impl<'a> DeclValidator<'a> {
206
199
return ;
207
200
}
208
201
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 ( ) ) ;
220
203
221
204
// Check whether non-snake case identifiers are allowed for this function.
222
205
if self . allowed ( func. into ( ) , allow:: NON_SNAKE_CASE , false ) {
@@ -231,6 +214,8 @@ impl<'a> DeclValidator<'a> {
231
214
expected_case : CaseType :: LowerSnakeCase ,
232
215
} ) ;
233
216
217
+ let body = self . db . body ( func. into ( ) ) ;
218
+
234
219
// Check the patterns inside the function body.
235
220
// This includes function parameters.
236
221
let pats_replacements = body
@@ -496,6 +481,11 @@ impl<'a> DeclValidator<'a> {
496
481
fn validate_enum ( & mut self , enum_id : EnumId ) {
497
482
let data = self . db . enum_data ( enum_id) ;
498
483
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
+
499
489
// Check whether non-camel case names are allowed for this enum.
500
490
if self . allowed ( enum_id. into ( ) , allow:: NON_CAMEL_CASE_TYPES , false ) {
501
491
return ;
@@ -512,13 +502,11 @@ impl<'a> DeclValidator<'a> {
512
502
// Check the field names.
513
503
let enum_fields_replacements = data
514
504
. variants
515
- . iter ( )
516
- . filter_map ( |( _ , variant) | {
505
+ . values ( )
506
+ . filter_map ( |variant| {
517
507
Some ( Replacement {
518
508
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 ( ) ) ?,
522
510
expected_case : CaseType :: UpperCamelCase ,
523
511
} )
524
512
} )
@@ -622,6 +610,8 @@ impl<'a> DeclValidator<'a> {
622
610
fn validate_const ( & mut self , const_id : ConstId ) {
623
611
let data = self . db . const_data ( const_id) ;
624
612
613
+ self . validate_body_inner_items ( const_id. into ( ) ) ;
614
+
625
615
if self . allowed ( const_id. into ( ) , allow:: NON_UPPER_CASE_GLOBAL , false ) {
626
616
return ;
627
617
}
@@ -631,7 +621,7 @@ impl<'a> DeclValidator<'a> {
631
621
None => return ,
632
622
} ;
633
623
634
- let const_name = name. display ( self . db . upcast ( ) ) . to_string ( ) ;
624
+ let const_name = name. to_smol_str ( ) ;
635
625
let replacement = if let Some ( new_name) = to_upper_snake_case ( & const_name) {
636
626
Replacement {
637
627
current_name : name. clone ( ) ,
@@ -670,13 +660,15 @@ impl<'a> DeclValidator<'a> {
670
660
return ;
671
661
}
672
662
663
+ self . validate_body_inner_items ( static_id. into ( ) ) ;
664
+
673
665
if self . allowed ( static_id. into ( ) , allow:: NON_UPPER_CASE_GLOBAL , false ) {
674
666
return ;
675
667
}
676
668
677
669
let name = & data. name ;
678
670
679
- let static_name = name. display ( self . db . upcast ( ) ) . to_string ( ) ;
671
+ let static_name = name. to_smol_str ( ) ;
680
672
let replacement = if let Some ( new_name) = to_upper_snake_case ( & static_name) {
681
673
Replacement {
682
674
current_name : name. clone ( ) ,
@@ -707,4 +699,17 @@ impl<'a> DeclValidator<'a> {
707
699
708
700
self . sink . push ( diagnostic) ;
709
701
}
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
+ }
710
715
}
0 commit comments