@@ -16,15 +16,14 @@ mod case_conv;
1616use std:: fmt;
1717
1818use hir_def:: {
19- data:: adt:: VariantData , db:: DefDatabase , hir:: Pat , src:: HasSource , AdtId , AttrDefId , ConstId ,
20- EnumId , EnumVariantId , FunctionId , HasModule , ItemContainerId , Lookup , ModuleDefId , ModuleId ,
21- StaticId , StructId , TraitId , TypeAliasId ,
19+ data:: adt:: VariantData , db:: DefDatabase , hir:: Pat , src:: HasSource , AdtId , ConstId , EnumId ,
20+ EnumVariantId , FunctionId , HasModule , ItemContainerId , Lookup , ModuleDefId , ModuleId , StaticId ,
21+ StructId , TraitId , TypeAliasId ,
2222} ;
2323use hir_expand:: {
2424 name:: { AsName , Name } ,
25- HirFileId , HirFileIdExt , MacroFileIdExt ,
25+ HirFileId , HirFileIdExt ,
2626} ;
27- use intern:: sym;
2827use stdx:: { always, never} ;
2928use syntax:: {
3029 ast:: { self , HasName } ,
@@ -36,14 +35,6 @@ use crate::db::HirDatabase;
3635
3736use self :: case_conv:: { to_camel_case, to_lower_snake_case, to_upper_snake_case} ;
3837
39- mod allow {
40- pub ( super ) const BAD_STYLE : & str = "bad_style" ;
41- pub ( super ) const NONSTANDARD_STYLE : & str = "nonstandard_style" ;
42- pub ( super ) const NON_SNAKE_CASE : & str = "non_snake_case" ;
43- pub ( super ) const NON_UPPER_CASE_GLOBAL : & str = "non_upper_case_globals" ;
44- pub ( super ) const NON_CAMEL_CASE_TYPES : & str = "non_camel_case_types" ;
45- }
46-
4738pub fn incorrect_case ( db : & dyn HirDatabase , owner : ModuleDefId ) -> Vec < IncorrectCase > {
4839 let _p = tracing:: info_span!( "incorrect_case" ) . entered ( ) ;
4940 let mut validator = DeclValidator :: new ( db) ;
@@ -160,92 +151,7 @@ impl<'a> DeclValidator<'a> {
160151 }
161152 }
162153
163- /// Checks whether not following the convention is allowed for this item.
164- fn allowed ( & self , id : AttrDefId , allow_name : & str , recursing : bool ) -> bool {
165- let is_allowed = |def_id| {
166- let attrs = self . db . attrs ( def_id) ;
167- // don't bug the user about directly no_mangle annotated stuff, they can't do anything about it
168- ( !recursing && attrs. by_key ( & sym:: no_mangle) . exists ( ) )
169- || attrs. by_key ( & sym:: allow) . tt_values ( ) . any ( |tt| {
170- let allows = tt. to_string ( ) ;
171- allows. contains ( allow_name)
172- || allows. contains ( allow:: BAD_STYLE )
173- || allows. contains ( allow:: NONSTANDARD_STYLE )
174- } )
175- } ;
176- let db = self . db . upcast ( ) ;
177- let file_id_is_derive = || {
178- match id {
179- AttrDefId :: ModuleId ( m) => {
180- m. def_map ( db) [ m. local_id ] . origin . file_id ( ) . map ( Into :: into)
181- }
182- AttrDefId :: FunctionId ( f) => Some ( f. lookup ( db) . id . file_id ( ) ) ,
183- AttrDefId :: StaticId ( sid) => Some ( sid. lookup ( db) . id . file_id ( ) ) ,
184- AttrDefId :: ConstId ( cid) => Some ( cid. lookup ( db) . id . file_id ( ) ) ,
185- AttrDefId :: TraitId ( tid) => Some ( tid. lookup ( db) . id . file_id ( ) ) ,
186- AttrDefId :: TraitAliasId ( taid) => Some ( taid. lookup ( db) . id . file_id ( ) ) ,
187- AttrDefId :: ImplId ( iid) => Some ( iid. lookup ( db) . id . file_id ( ) ) ,
188- AttrDefId :: ExternBlockId ( id) => Some ( id. lookup ( db) . id . file_id ( ) ) ,
189- AttrDefId :: ExternCrateId ( id) => Some ( id. lookup ( db) . id . file_id ( ) ) ,
190- AttrDefId :: UseId ( id) => Some ( id. lookup ( db) . id . file_id ( ) ) ,
191- // These warnings should not explore macro definitions at all
192- AttrDefId :: MacroId ( _) => None ,
193- AttrDefId :: AdtId ( aid) => match aid {
194- AdtId :: StructId ( sid) => Some ( sid. lookup ( db) . id . file_id ( ) ) ,
195- AdtId :: EnumId ( eid) => Some ( eid. lookup ( db) . id . file_id ( ) ) ,
196- // Unions aren't yet supported
197- AdtId :: UnionId ( _) => None ,
198- } ,
199- AttrDefId :: FieldId ( _) => None ,
200- AttrDefId :: EnumVariantId ( _) => None ,
201- AttrDefId :: TypeAliasId ( _) => None ,
202- AttrDefId :: GenericParamId ( _) => None ,
203- }
204- . map_or ( false , |file_id| {
205- matches ! ( file_id. macro_file( ) , Some ( file_id) if file_id. is_custom_derive( db. upcast( ) ) || file_id. is_builtin_derive( db. upcast( ) ) )
206- } )
207- } ;
208-
209- let parent = || {
210- match id {
211- AttrDefId :: ModuleId ( m) => m. containing_module ( db) . map ( |v| v. into ( ) ) ,
212- AttrDefId :: FunctionId ( f) => Some ( f. lookup ( db) . container . into ( ) ) ,
213- AttrDefId :: StaticId ( sid) => Some ( sid. lookup ( db) . container . into ( ) ) ,
214- AttrDefId :: ConstId ( cid) => Some ( cid. lookup ( db) . container . into ( ) ) ,
215- AttrDefId :: TraitId ( tid) => Some ( tid. lookup ( db) . container . into ( ) ) ,
216- AttrDefId :: TraitAliasId ( taid) => Some ( taid. lookup ( db) . container . into ( ) ) ,
217- AttrDefId :: ImplId ( iid) => Some ( iid. lookup ( db) . container . into ( ) ) ,
218- AttrDefId :: ExternBlockId ( id) => Some ( id. lookup ( db) . container . into ( ) ) ,
219- AttrDefId :: ExternCrateId ( id) => Some ( id. lookup ( db) . container . into ( ) ) ,
220- AttrDefId :: UseId ( id) => Some ( id. lookup ( db) . container . into ( ) ) ,
221- // These warnings should not explore macro definitions at all
222- AttrDefId :: MacroId ( _) => None ,
223- AttrDefId :: AdtId ( aid) => match aid {
224- AdtId :: StructId ( sid) => Some ( sid. lookup ( db) . container . into ( ) ) ,
225- AdtId :: EnumId ( eid) => Some ( eid. lookup ( db) . container . into ( ) ) ,
226- // Unions aren't yet supported
227- AdtId :: UnionId ( _) => None ,
228- } ,
229- AttrDefId :: FieldId ( _) => None ,
230- AttrDefId :: EnumVariantId ( _) => None ,
231- AttrDefId :: TypeAliasId ( _) => None ,
232- AttrDefId :: GenericParamId ( _) => None ,
233- }
234- . is_some_and ( |mid| self . allowed ( mid, allow_name, true ) )
235- } ;
236- is_allowed ( id)
237- // FIXME: this is a hack to avoid false positives in derive macros currently
238- || file_id_is_derive ( )
239- // go upwards one step or give up
240- || parent ( )
241- }
242-
243154 fn validate_module ( & mut self , module_id : ModuleId ) {
244- // Check whether non-snake case identifiers are allowed for this module.
245- if self . allowed ( module_id. into ( ) , allow:: NON_SNAKE_CASE , false ) {
246- return ;
247- }
248-
249155 // Check the module name.
250156 let Some ( module_name) = module_id. name ( self . db . upcast ( ) ) else { return } ;
251157 let Some ( module_name_replacement) =
@@ -270,11 +176,6 @@ impl<'a> DeclValidator<'a> {
270176 }
271177
272178 fn validate_trait ( & mut self , trait_id : TraitId ) {
273- // Check whether non-snake case identifiers are allowed for this trait.
274- if self . allowed ( trait_id. into ( ) , allow:: NON_CAMEL_CASE_TYPES , false ) {
275- return ;
276- }
277-
278179 // Check the trait name.
279180 let data = self . db . trait_data ( trait_id) ;
280181 self . create_incorrect_case_diagnostic_for_item_name (
@@ -292,11 +193,6 @@ impl<'a> DeclValidator<'a> {
292193 return ;
293194 }
294195
295- // Check whether non-snake case identifiers are allowed for this function.
296- if self . allowed ( func. into ( ) , allow:: NON_SNAKE_CASE , false ) {
297- return ;
298- }
299-
300196 // Check the function name.
301197 // Skipped if function is an associated item of a trait implementation.
302198 if !self . is_trait_impl_container ( container) {
@@ -389,28 +285,20 @@ impl<'a> DeclValidator<'a> {
389285
390286 fn validate_struct ( & mut self , struct_id : StructId ) {
391287 // Check the structure name.
392- let non_camel_case_allowed =
393- self . allowed ( struct_id. into ( ) , allow:: NON_CAMEL_CASE_TYPES , false ) ;
394- if !non_camel_case_allowed {
395- let data = self . db . struct_data ( struct_id) ;
396- self . create_incorrect_case_diagnostic_for_item_name (
397- struct_id,
398- & data. name ,
399- CaseType :: UpperCamelCase ,
400- IdentType :: Structure ,
401- ) ;
402- }
288+ let data = self . db . struct_data ( struct_id) ;
289+ self . create_incorrect_case_diagnostic_for_item_name (
290+ struct_id,
291+ & data. name ,
292+ CaseType :: UpperCamelCase ,
293+ IdentType :: Structure ,
294+ ) ;
403295
404296 // Check the field names.
405297 self . validate_struct_fields ( struct_id) ;
406298 }
407299
408300 /// Check incorrect names for struct fields.
409301 fn validate_struct_fields ( & mut self , struct_id : StructId ) {
410- if self . allowed ( struct_id. into ( ) , allow:: NON_SNAKE_CASE , false ) {
411- return ;
412- }
413-
414302 let data = self . db . struct_data ( struct_id) ;
415303 let VariantData :: Record ( fields) = data. variant_data . as_ref ( ) else {
416304 return ;
@@ -484,11 +372,6 @@ impl<'a> DeclValidator<'a> {
484372 fn validate_enum ( & mut self , enum_id : EnumId ) {
485373 let data = self . db . enum_data ( enum_id) ;
486374
487- // Check whether non-camel case names are allowed for this enum.
488- if self . allowed ( enum_id. into ( ) , allow:: NON_CAMEL_CASE_TYPES , false ) {
489- return ;
490- }
491-
492375 // Check the enum name.
493376 self . create_incorrect_case_diagnostic_for_item_name (
494377 enum_id,
@@ -653,10 +536,6 @@ impl<'a> DeclValidator<'a> {
653536 return ;
654537 }
655538
656- if self . allowed ( const_id. into ( ) , allow:: NON_UPPER_CASE_GLOBAL , false ) {
657- return ;
658- }
659-
660539 let data = self . db . const_data ( const_id) ;
661540 let Some ( name) = & data. name else {
662541 return ;
@@ -676,10 +555,6 @@ impl<'a> DeclValidator<'a> {
676555 return ;
677556 }
678557
679- if self . allowed ( static_id. into ( ) , allow:: NON_UPPER_CASE_GLOBAL , false ) {
680- return ;
681- }
682-
683558 self . create_incorrect_case_diagnostic_for_item_name (
684559 static_id,
685560 & data. name ,
@@ -695,11 +570,6 @@ impl<'a> DeclValidator<'a> {
695570 return ;
696571 }
697572
698- // Check whether non-snake case identifiers are allowed for this type alias.
699- if self . allowed ( type_alias_id. into ( ) , allow:: NON_CAMEL_CASE_TYPES , false ) {
700- return ;
701- }
702-
703573 // Check the type alias name.
704574 let data = self . db . type_alias_data ( type_alias_id) ;
705575 self . create_incorrect_case_diagnostic_for_item_name (
0 commit comments