2
2
3
3
use clippy_utils:: diagnostics:: { span_lint, span_lint_and_help, span_lint_hir} ;
4
4
use clippy_utils:: source:: is_present_in_source;
5
- use clippy_utils:: str_utils:: { camel_case_split, count_match_end, count_match_start, to_snake_case} ;
5
+ use clippy_utils:: str_utils:: { camel_case_split, count_match_end, count_match_start, to_camel_case , to_snake_case} ;
6
6
use rustc_hir:: { EnumDef , FieldDef , Item , ItemKind , OwnerId , Variant , VariantData } ;
7
7
use rustc_lint:: { LateContext , LateLintPass } ;
8
8
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
@@ -165,48 +165,20 @@ impl_lint_pass!(ItemNameRepetitions => [
165
165
MODULE_INCEPTION
166
166
] ) ;
167
167
168
- fn is_snake_case ( name : & str ) -> bool {
169
- name. chars ( ) . all ( |c| !c. is_uppercase ( ) || c == '_' )
170
- }
171
-
172
168
#[ must_use]
173
169
fn have_no_extra_prefix ( prefixes : & [ & str ] ) -> bool {
174
170
prefixes. iter ( ) . all ( |p| p == & "" || p == & "_" )
175
171
}
176
172
177
- #[ must_use]
178
- fn to_camel_case ( item_name : & str ) -> String {
179
- let mut s = String :: new ( ) ;
180
- let mut up = true ;
181
- for c in item_name. chars ( ) {
182
- if c. is_uppercase ( ) {
183
- // we only turn snake case text into CamelCase
184
- return item_name. to_string ( ) ;
185
- }
186
- if c == '_' {
187
- up = true ;
188
- continue ;
189
- }
190
- if up {
191
- up = false ;
192
- s. extend ( c. to_uppercase ( ) ) ;
193
- } else {
194
- s. push ( c) ;
195
- }
196
- }
197
- s
198
- }
199
-
200
173
fn check_fields ( cx : & LateContext < ' _ > , threshold : u64 , fields : & [ FieldDef < ' _ > ] , item_name : & str , item_span : Span ) {
201
174
if ( fields. len ( ) as u64 ) < threshold {
202
175
return ;
203
176
}
204
177
178
+ let item_name_snake = to_snake_case ( item_name) ;
205
179
for field in fields {
206
- if is_snake_case ( field. ident . name . as_str ( ) ) {
207
- check_struct_start ( cx, item_name, field) ;
208
- check_struct_end ( cx, item_name, field) ;
209
- }
180
+ check_struct_start ( cx, & item_name_snake, field) ;
181
+ check_struct_end ( cx, & item_name_snake, field) ;
210
182
}
211
183
212
184
let mut pre: Vec < & str > = match fields. get ( 0 ) {
@@ -255,10 +227,9 @@ fn check_fields(cx: &LateContext<'_>, threshold: u64, fields: &[FieldDef<'_>], i
255
227
}
256
228
257
229
fn check_struct_start ( cx : & LateContext < ' _ > , item_name : & str , field : & FieldDef < ' _ > ) {
258
- let item_name_snake = to_snake_case ( item_name) ;
259
230
let name = field. ident . name . as_str ( ) ;
260
231
261
- if name. starts_with ( & item_name_snake ) {
232
+ if name. starts_with ( item_name ) {
262
233
span_lint_hir (
263
234
cx,
264
235
STRUCT_FIELD_NAMES ,
@@ -270,10 +241,9 @@ fn check_struct_start(cx: &LateContext<'_>, item_name: &str, field: &FieldDef<'_
270
241
}
271
242
272
243
fn check_struct_end ( cx : & LateContext < ' _ > , item_name : & str , field : & FieldDef < ' _ > ) {
273
- let item_name_snake = to_snake_case ( item_name) ;
274
244
let name = field. ident . name . as_str ( ) ;
275
245
276
- if name. ends_with ( & item_name_snake ) {
246
+ if name. ends_with ( item_name ) {
277
247
span_lint_hir (
278
248
cx,
279
249
STRUCT_FIELD_NAMES ,
0 commit comments