@@ -20,7 +20,7 @@ use hir_def::{
20
20
hir:: { Pat , PatId } ,
21
21
src:: HasSource ,
22
22
AdtId , AttrDefId , ConstId , EnumId , FunctionId , ItemContainerId , Lookup , ModuleDefId , ModuleId ,
23
- StaticId , StructId ,
23
+ StaticId , StructId , TraitId ,
24
24
} ;
25
25
use hir_expand:: {
26
26
name:: { AsName , Name } ,
@@ -79,12 +79,13 @@ pub enum IdentType {
79
79
Enum ,
80
80
Field ,
81
81
Function ,
82
+ Module ,
82
83
Parameter ,
83
84
StaticVariable ,
84
85
Structure ,
86
+ Trait ,
85
87
Variable ,
86
88
Variant ,
87
- Module ,
88
89
}
89
90
90
91
impl fmt:: Display for IdentType {
@@ -94,12 +95,13 @@ impl fmt::Display for IdentType {
94
95
IdentType :: Enum => "Enum" ,
95
96
IdentType :: Field => "Field" ,
96
97
IdentType :: Function => "Function" ,
98
+ IdentType :: Module => "Module" ,
97
99
IdentType :: Parameter => "Parameter" ,
98
100
IdentType :: StaticVariable => "Static variable" ,
99
101
IdentType :: Structure => "Structure" ,
102
+ IdentType :: Trait => "Trait" ,
100
103
IdentType :: Variable => "Variable" ,
101
104
IdentType :: Variant => "Variant" ,
102
- IdentType :: Module => "Module" ,
103
105
} ;
104
106
105
107
repr. fmt ( f)
@@ -136,6 +138,7 @@ impl<'a> DeclValidator<'a> {
136
138
pub ( super ) fn validate_item ( & mut self , item : ModuleDefId ) {
137
139
match item {
138
140
ModuleDefId :: ModuleId ( module_id) => self . validate_module ( module_id) ,
141
+ ModuleDefId :: TraitId ( trait_id) => self . validate_trait ( trait_id) ,
139
142
ModuleDefId :: FunctionId ( func) => self . validate_func ( func) ,
140
143
ModuleDefId :: AdtId ( adt) => self . validate_adt ( adt) ,
141
144
ModuleDefId :: ConstId ( const_id) => self . validate_const ( const_id) ,
@@ -283,6 +286,47 @@ impl<'a> DeclValidator<'a> {
283
286
}
284
287
}
285
288
289
+ fn validate_trait ( & mut self , trait_id : TraitId ) {
290
+ // Check whether non-snake case identifiers are allowed for this trait.
291
+ if self . allowed ( trait_id. into ( ) , allow:: NON_CAMEL_CASE_TYPES , false ) {
292
+ return ;
293
+ }
294
+
295
+ // Check the trait name.
296
+ let data = self . db . trait_data ( trait_id) ;
297
+ let trait_name = data. name . display ( self . db . upcast ( ) ) . to_string ( ) ;
298
+ let trait_name_replacement = to_camel_case ( & trait_name) . map ( |new_name| Replacement {
299
+ current_name : data. name . clone ( ) ,
300
+ suggested_text : new_name,
301
+ expected_case : CaseType :: UpperCamelCase ,
302
+ } ) ;
303
+
304
+ if let Some ( replacement) = trait_name_replacement {
305
+ let trait_loc = trait_id. lookup ( self . db . upcast ( ) ) ;
306
+ let trait_src = trait_loc. source ( self . db . upcast ( ) ) ;
307
+
308
+ let Some ( ast_ptr) = trait_src. value . name ( ) else {
309
+ never ! (
310
+ "Replacement ({:?}) was generated for a trait without a name: {:?}" ,
311
+ replacement,
312
+ trait_src
313
+ ) ;
314
+ return ;
315
+ } ;
316
+
317
+ let diagnostic = IncorrectCase {
318
+ file : trait_src. file_id ,
319
+ ident_type : IdentType :: Trait ,
320
+ ident : AstPtr :: new ( & ast_ptr) ,
321
+ expected_case : replacement. expected_case ,
322
+ ident_text : trait_name,
323
+ suggested_text : replacement. suggested_text ,
324
+ } ;
325
+
326
+ self . sink . push ( diagnostic) ;
327
+ }
328
+ }
329
+
286
330
fn validate_func ( & mut self , func : FunctionId ) {
287
331
let data = self . db . function_data ( func) ;
288
332
if matches ! ( func. lookup( self . db. upcast( ) ) . container, ItemContainerId :: ExternBlockId ( _) ) {
0 commit comments