@@ -16,15 +16,14 @@ mod case_conv;
16
16
use std:: fmt;
17
17
18
18
use 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 ,
22
22
} ;
23
23
use hir_expand:: {
24
24
name:: { AsName , Name } ,
25
- HirFileId , HirFileIdExt , MacroFileIdExt ,
25
+ HirFileId , HirFileIdExt ,
26
26
} ;
27
- use intern:: sym;
28
27
use stdx:: { always, never} ;
29
28
use syntax:: {
30
29
ast:: { self , HasName } ,
@@ -36,14 +35,6 @@ use crate::db::HirDatabase;
36
35
37
36
use self :: case_conv:: { to_camel_case, to_lower_snake_case, to_upper_snake_case} ;
38
37
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
-
47
38
pub fn incorrect_case ( db : & dyn HirDatabase , owner : ModuleDefId ) -> Vec < IncorrectCase > {
48
39
let _p = tracing:: info_span!( "incorrect_case" ) . entered ( ) ;
49
40
let mut validator = DeclValidator :: new ( db) ;
@@ -160,92 +151,7 @@ impl<'a> DeclValidator<'a> {
160
151
}
161
152
}
162
153
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
-
243
154
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
-
249
155
// Check the module name.
250
156
let Some ( module_name) = module_id. name ( self . db . upcast ( ) ) else { return } ;
251
157
let Some ( module_name_replacement) =
@@ -270,11 +176,6 @@ impl<'a> DeclValidator<'a> {
270
176
}
271
177
272
178
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
-
278
179
// Check the trait name.
279
180
let data = self . db . trait_data ( trait_id) ;
280
181
self . create_incorrect_case_diagnostic_for_item_name (
@@ -292,11 +193,6 @@ impl<'a> DeclValidator<'a> {
292
193
return ;
293
194
}
294
195
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
-
300
196
// Check the function name.
301
197
// Skipped if function is an associated item of a trait implementation.
302
198
if !self . is_trait_impl_container ( container) {
@@ -389,28 +285,20 @@ impl<'a> DeclValidator<'a> {
389
285
390
286
fn validate_struct ( & mut self , struct_id : StructId ) {
391
287
// 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
+ ) ;
403
295
404
296
// Check the field names.
405
297
self . validate_struct_fields ( struct_id) ;
406
298
}
407
299
408
300
/// Check incorrect names for struct fields.
409
301
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
-
414
302
let data = self . db . struct_data ( struct_id) ;
415
303
let VariantData :: Record ( fields) = data. variant_data . as_ref ( ) else {
416
304
return ;
@@ -484,11 +372,6 @@ impl<'a> DeclValidator<'a> {
484
372
fn validate_enum ( & mut self , enum_id : EnumId ) {
485
373
let data = self . db . enum_data ( enum_id) ;
486
374
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
-
492
375
// Check the enum name.
493
376
self . create_incorrect_case_diagnostic_for_item_name (
494
377
enum_id,
@@ -653,10 +536,6 @@ impl<'a> DeclValidator<'a> {
653
536
return ;
654
537
}
655
538
656
- if self . allowed ( const_id. into ( ) , allow:: NON_UPPER_CASE_GLOBAL , false ) {
657
- return ;
658
- }
659
-
660
539
let data = self . db . const_data ( const_id) ;
661
540
let Some ( name) = & data. name else {
662
541
return ;
@@ -676,10 +555,6 @@ impl<'a> DeclValidator<'a> {
676
555
return ;
677
556
}
678
557
679
- if self . allowed ( static_id. into ( ) , allow:: NON_UPPER_CASE_GLOBAL , false ) {
680
- return ;
681
- }
682
-
683
558
self . create_incorrect_case_diagnostic_for_item_name (
684
559
static_id,
685
560
& data. name ,
@@ -695,11 +570,6 @@ impl<'a> DeclValidator<'a> {
695
570
return ;
696
571
}
697
572
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
-
703
573
// Check the type alias name.
704
574
let data = self . db . type_alias_data ( type_alias_id) ;
705
575
self . create_incorrect_case_diagnostic_for_item_name (
0 commit comments