@@ -24,19 +24,21 @@ use rustc::hir::intravisit::FnKind;
24
24
pub enum MethodLateContext {
25
25
TraitDefaultImpl ,
26
26
TraitImpl ,
27
- PlainImpl
27
+ PlainImpl ,
28
28
}
29
29
30
30
pub fn method_context ( cx : & LateContext , id : ast:: NodeId , span : Span ) -> MethodLateContext {
31
31
let def_id = cx. tcx . map . local_def_id ( id) ;
32
32
match cx. tcx . impl_or_trait_items . borrow ( ) . get ( & def_id) {
33
33
None => span_bug ! ( span, "missing method descriptor?!" ) ,
34
- Some ( item) => match item. container ( ) {
35
- ty:: TraitContainer ( ..) => MethodLateContext :: TraitDefaultImpl ,
36
- ty:: ImplContainer ( cid) => {
37
- match cx. tcx . impl_trait_ref ( cid) {
38
- Some ( _) => MethodLateContext :: TraitImpl ,
39
- None => MethodLateContext :: PlainImpl
34
+ Some ( item) => {
35
+ match item. container ( ) {
36
+ ty:: TraitContainer ( ..) => MethodLateContext :: TraitDefaultImpl ,
37
+ ty:: ImplContainer ( cid) => {
38
+ match cx. tcx . impl_trait_ref ( cid) {
39
+ Some ( _) => MethodLateContext :: TraitImpl ,
40
+ None => MethodLateContext :: PlainImpl ,
41
+ }
40
42
}
41
43
}
42
44
}
@@ -63,29 +65,35 @@ impl NonCamelCaseTypes {
63
65
64
66
// start with a non-lowercase letter rather than non-uppercase
65
67
// ones (some scripts don't have a concept of upper/lowercase)
66
- !name. is_empty ( ) &&
67
- !name. chars ( ) . next ( ) . unwrap ( ) . is_lowercase ( ) &&
68
- !name. contains ( '_' )
68
+ !name. is_empty ( ) && !name. chars ( ) . next ( ) . unwrap ( ) . is_lowercase ( ) && !name. contains ( '_' )
69
69
}
70
70
71
71
fn to_camel_case ( s : & str ) -> String {
72
- s. split ( '_' ) . flat_map ( |word| word. chars ( ) . enumerate ( ) . map ( |( i, c) |
73
- if i == 0 {
74
- c. to_uppercase ( ) . collect :: < String > ( )
75
- } else {
76
- c. to_lowercase ( ) . collect ( )
77
- }
78
- ) ) . collect :: < Vec < _ > > ( ) . concat ( )
72
+ s. split ( '_' )
73
+ . flat_map ( |word| {
74
+ word. chars ( ) . enumerate ( ) . map ( |( i, c) | if i == 0 {
75
+ c. to_uppercase ( ) . collect :: < String > ( )
76
+ } else {
77
+ c. to_lowercase ( ) . collect ( )
78
+ } )
79
+ } )
80
+ . collect :: < Vec < _ > > ( )
81
+ . concat ( )
79
82
}
80
83
81
84
let s = name. as_str ( ) ;
82
85
83
86
if !is_camel_case ( name) {
84
87
let c = to_camel_case ( & s) ;
85
88
let m = if c. is_empty ( ) {
86
- format ! ( "{} `{}` should have a camel case name such as `CamelCase`" , sort, s)
89
+ format ! ( "{} `{}` should have a camel case name such as `CamelCase`" ,
90
+ sort,
91
+ s)
87
92
} else {
88
- format ! ( "{} `{}` should have a camel case name such as `{}`" , sort, s, c)
93
+ format ! ( "{} `{}` should have a camel case name such as `{}`" ,
94
+ sort,
95
+ s,
96
+ c)
89
97
} ;
90
98
cx. span_lint ( NON_CAMEL_CASE_TYPES , span, & m[ ..] ) ;
91
99
}
@@ -100,23 +108,25 @@ impl LintPass for NonCamelCaseTypes {
100
108
101
109
impl LateLintPass for NonCamelCaseTypes {
102
110
fn check_item ( & mut self , cx : & LateContext , it : & hir:: Item ) {
103
- let extern_repr_count = it. attrs . iter ( ) . filter ( |attr| {
104
- attr:: find_repr_attrs ( cx. tcx . sess . diagnostic ( ) , attr) . iter ( )
105
- . any ( |r| r == & attr:: ReprExtern )
106
- } ) . count ( ) ;
111
+ let extern_repr_count = it. attrs
112
+ . iter ( )
113
+ . filter ( |attr| {
114
+ attr:: find_repr_attrs ( cx. tcx . sess . diagnostic ( ) , attr)
115
+ . iter ( )
116
+ . any ( |r| r == & attr:: ReprExtern )
117
+ } )
118
+ . count ( ) ;
107
119
let has_extern_repr = extern_repr_count > 0 ;
108
120
109
121
if has_extern_repr {
110
122
return ;
111
123
}
112
124
113
125
match it. node {
114
- hir:: ItemTy ( ..) | hir:: ItemStruct ( ..) | hir:: ItemUnion ( ..) => {
115
- self . check_case ( cx, "type" , it. name , it. span )
116
- }
117
- hir:: ItemTrait ( ..) => {
118
- self . check_case ( cx, "trait" , it. name , it. span )
119
- }
126
+ hir:: ItemTy ( ..) |
127
+ hir:: ItemStruct ( ..) |
128
+ hir:: ItemUnion ( ..) => self . check_case ( cx, "type" , it. name , it. span ) ,
129
+ hir:: ItemTrait ( ..) => self . check_case ( cx, "trait" , it. name , it. span ) ,
120
130
hir:: ItemEnum ( ref enum_definition, _) => {
121
131
if has_extern_repr {
122
132
return ;
@@ -126,7 +136,7 @@ impl LateLintPass for NonCamelCaseTypes {
126
136
self . check_case ( cx, "variant" , variant. node . name , variant. span ) ;
127
137
}
128
138
}
129
- _ => ( )
139
+ _ => ( ) ,
130
140
}
131
141
}
132
142
@@ -165,9 +175,7 @@ impl NonSnakeCase {
165
175
continue ;
166
176
}
167
177
for ch in s. chars ( ) {
168
- if !buf. is_empty ( ) && buf != "'"
169
- && ch. is_uppercase ( )
170
- && !last_upper {
178
+ if !buf. is_empty ( ) && buf != "'" && ch. is_uppercase ( ) && !last_upper {
171
179
words. push ( buf) ;
172
180
buf = String :: new ( ) ;
173
181
}
@@ -205,10 +213,11 @@ impl NonSnakeCase {
205
213
let sc = NonSnakeCase :: to_snake_case ( name) ;
206
214
let msg = if sc != name {
207
215
format ! ( "{} `{}` should have a snake case name such as `{}`" ,
208
- sort, name, sc)
216
+ sort,
217
+ name,
218
+ sc)
209
219
} else {
210
- format ! ( "{} `{}` should have a snake case name" ,
211
- sort, name)
220
+ format ! ( "{} `{}` should have a snake case name" , sort, name)
212
221
} ;
213
222
match span {
214
223
Some ( span) => cx. span_lint ( NON_SNAKE_CASE , span, & msg) ,
@@ -226,31 +235,39 @@ impl LintPass for NonSnakeCase {
226
235
227
236
impl LateLintPass for NonSnakeCase {
228
237
fn check_crate ( & mut self , cx : & LateContext , cr : & hir:: Crate ) {
229
- let attr_crate_name = cr. attrs . iter ( ) . find ( |at| at. check_name ( "crate_name" ) )
230
- . and_then ( |at| at. value_str ( ) . map ( |s| ( at, s) ) ) ;
238
+ let attr_crate_name = cr. attrs
239
+ . iter ( )
240
+ . find ( |at| at. check_name ( "crate_name" ) )
241
+ . and_then ( |at| at. value_str ( ) . map ( |s| ( at, s) ) ) ;
231
242
if let Some ( ref name) = cx. tcx . sess . opts . crate_name {
232
243
self . check_snake_case ( cx, "crate" , name, None ) ;
233
244
} else if let Some ( ( attr, ref name) ) = attr_crate_name {
234
245
self . check_snake_case ( cx, "crate" , name, Some ( attr. span ) ) ;
235
246
}
236
247
}
237
248
238
- fn check_fn ( & mut self , cx : & LateContext ,
239
- fk : FnKind , _: & hir:: FnDecl ,
240
- _: & hir:: Block , span : Span , id : ast:: NodeId ) {
249
+ fn check_fn ( & mut self ,
250
+ cx : & LateContext ,
251
+ fk : FnKind ,
252
+ _: & hir:: FnDecl ,
253
+ _: & hir:: Block ,
254
+ span : Span ,
255
+ id : ast:: NodeId ) {
241
256
match fk {
242
- FnKind :: Method ( name, ..) => match method_context ( cx, id, span) {
243
- MethodLateContext :: PlainImpl => {
244
- self . check_snake_case ( cx, "method" , & name. as_str ( ) , Some ( span) )
245
- } ,
246
- MethodLateContext :: TraitDefaultImpl => {
247
- self . check_snake_case ( cx, "trait method" , & name. as_str ( ) , Some ( span) )
248
- } ,
249
- _ => ( ) ,
250
- } ,
257
+ FnKind :: Method ( name, ..) => {
258
+ match method_context ( cx, id, span) {
259
+ MethodLateContext :: PlainImpl => {
260
+ self . check_snake_case ( cx, "method" , & name. as_str ( ) , Some ( span) )
261
+ }
262
+ MethodLateContext :: TraitDefaultImpl => {
263
+ self . check_snake_case ( cx, "trait method" , & name. as_str ( ) , Some ( span) )
264
+ }
265
+ _ => ( ) ,
266
+ }
267
+ }
251
268
FnKind :: ItemFn ( name, ..) => {
252
269
self . check_snake_case ( cx, "function" , & name. as_str ( ) , Some ( span) )
253
- } ,
270
+ }
254
271
FnKind :: Closure ( _) => ( ) ,
255
272
}
256
273
}
@@ -263,13 +280,17 @@ impl LateLintPass for NonSnakeCase {
263
280
264
281
fn check_trait_item ( & mut self , cx : & LateContext , trait_item : & hir:: TraitItem ) {
265
282
if let hir:: MethodTraitItem ( _, None ) = trait_item. node {
266
- self . check_snake_case ( cx, "trait method" , & trait_item. name . as_str ( ) ,
283
+ self . check_snake_case ( cx,
284
+ "trait method" ,
285
+ & trait_item. name . as_str ( ) ,
267
286
Some ( trait_item. span ) ) ;
268
287
}
269
288
}
270
289
271
290
fn check_lifetime_def ( & mut self , cx : & LateContext , t : & hir:: LifetimeDef ) {
272
- self . check_snake_case ( cx, "lifetime" , & t. lifetime . name . as_str ( ) ,
291
+ self . check_snake_case ( cx,
292
+ "lifetime" ,
293
+ & t. lifetime . name . as_str ( ) ,
273
294
Some ( t. lifetime . span ) ) ;
274
295
}
275
296
@@ -282,8 +303,12 @@ impl LateLintPass for NonSnakeCase {
282
303
}
283
304
}
284
305
285
- fn check_struct_def ( & mut self , cx : & LateContext , s : & hir:: VariantData ,
286
- _: ast:: Name , _: & hir:: Generics , _: ast:: NodeId ) {
306
+ fn check_struct_def ( & mut self ,
307
+ cx : & LateContext ,
308
+ s : & hir:: VariantData ,
309
+ _: ast:: Name ,
310
+ _: & hir:: Generics ,
311
+ _: ast:: NodeId ) {
287
312
for sf in s. fields ( ) {
288
313
self . check_snake_case ( cx, "structure field" , & sf. name . as_str ( ) , Some ( sf. span ) ) ;
289
314
}
@@ -306,13 +331,16 @@ impl NonUpperCaseGlobals {
306
331
if s. chars ( ) . any ( |c| c. is_lowercase ( ) ) {
307
332
let uc = NonSnakeCase :: to_snake_case ( & s) . to_uppercase ( ) ;
308
333
if uc != & s[ ..] {
309
- cx. span_lint ( NON_UPPER_CASE_GLOBALS , span,
310
- & format ! ( "{} `{}` should have an upper case name such as `{}`" ,
311
- sort, s, uc) ) ;
334
+ cx. span_lint ( NON_UPPER_CASE_GLOBALS ,
335
+ span,
336
+ & format ! ( "{} `{}` should have an upper case name such as `{}`" ,
337
+ sort,
338
+ s,
339
+ uc) ) ;
312
340
} else {
313
- cx. span_lint ( NON_UPPER_CASE_GLOBALS , span ,
314
- & format ! ( "{} `{}` should have an upper case name" ,
315
- sort, s) ) ;
341
+ cx. span_lint ( NON_UPPER_CASE_GLOBALS ,
342
+ span ,
343
+ & format ! ( "{} `{}` should have an upper case name" , sort, s) ) ;
316
344
}
317
345
}
318
346
}
@@ -341,8 +369,7 @@ impl LateLintPass for NonUpperCaseGlobals {
341
369
fn check_trait_item ( & mut self , cx : & LateContext , ti : & hir:: TraitItem ) {
342
370
match ti. node {
343
371
hir:: ConstTraitItem ( ..) => {
344
- NonUpperCaseGlobals :: check_upper_case ( cx, "associated constant" ,
345
- ti. name , ti. span ) ;
372
+ NonUpperCaseGlobals :: check_upper_case ( cx, "associated constant" , ti. name , ti. span ) ;
346
373
}
347
374
_ => { }
348
375
}
@@ -351,8 +378,7 @@ impl LateLintPass for NonUpperCaseGlobals {
351
378
fn check_impl_item ( & mut self , cx : & LateContext , ii : & hir:: ImplItem ) {
352
379
match ii. node {
353
380
hir:: ImplItemKind :: Const ( ..) => {
354
- NonUpperCaseGlobals :: check_upper_case ( cx, "associated constant" ,
355
- ii. name , ii. span ) ;
381
+ NonUpperCaseGlobals :: check_upper_case ( cx, "associated constant" , ii. name , ii. span ) ;
356
382
}
357
383
_ => { }
358
384
}
@@ -363,8 +389,10 @@ impl LateLintPass for NonUpperCaseGlobals {
363
389
if let PatKind :: Path ( None , ref path) = p. node {
364
390
if !path. global && path. segments . len ( ) == 1 && path. segments [ 0 ] . parameters . is_empty ( ) {
365
391
if let Def :: Const ( ..) = cx. tcx . expect_def ( p. id ) {
366
- NonUpperCaseGlobals :: check_upper_case ( cx, "constant in pattern" ,
367
- path. segments [ 0 ] . name , path. span ) ;
392
+ NonUpperCaseGlobals :: check_upper_case ( cx,
393
+ "constant in pattern" ,
394
+ path. segments [ 0 ] . name ,
395
+ path. span ) ;
368
396
}
369
397
}
370
398
}
0 commit comments