@@ -24,19 +24,21 @@ use rustc::hir::intravisit::FnKind;
2424pub enum MethodLateContext {
2525 TraitDefaultImpl ,
2626 TraitImpl ,
27- PlainImpl
27+ PlainImpl ,
2828}
2929
3030pub fn method_context ( cx : & LateContext , id : ast:: NodeId , span : Span ) -> MethodLateContext {
3131 let def_id = cx. tcx . map . local_def_id ( id) ;
3232 match cx. tcx . impl_or_trait_items . borrow ( ) . get ( & def_id) {
3333 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+ }
4042 }
4143 }
4244 }
@@ -63,29 +65,35 @@ impl NonCamelCaseTypes {
6365
6466 // start with a non-lowercase letter rather than non-uppercase
6567 // 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 ( '_' )
6969 }
7070
7171 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 ( )
7982 }
8083
8184 let s = name. as_str ( ) ;
8285
8386 if !is_camel_case ( name) {
8487 let c = to_camel_case ( & s) ;
8588 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)
8792 } 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)
8997 } ;
9098 cx. span_lint ( NON_CAMEL_CASE_TYPES , span, & m[ ..] ) ;
9199 }
@@ -100,23 +108,25 @@ impl LintPass for NonCamelCaseTypes {
100108
101109impl LateLintPass for NonCamelCaseTypes {
102110 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 ( ) ;
107119 let has_extern_repr = extern_repr_count > 0 ;
108120
109121 if has_extern_repr {
110122 return ;
111123 }
112124
113125 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 ) ,
120130 hir:: ItemEnum ( ref enum_definition, _) => {
121131 if has_extern_repr {
122132 return ;
@@ -126,7 +136,7 @@ impl LateLintPass for NonCamelCaseTypes {
126136 self . check_case ( cx, "variant" , variant. node . name , variant. span ) ;
127137 }
128138 }
129- _ => ( )
139+ _ => ( ) ,
130140 }
131141 }
132142
@@ -165,9 +175,7 @@ impl NonSnakeCase {
165175 continue ;
166176 }
167177 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 {
171179 words. push ( buf) ;
172180 buf = String :: new ( ) ;
173181 }
@@ -205,10 +213,11 @@ impl NonSnakeCase {
205213 let sc = NonSnakeCase :: to_snake_case ( name) ;
206214 let msg = if sc != name {
207215 format ! ( "{} `{}` should have a snake case name such as `{}`" ,
208- sort, name, sc)
216+ sort,
217+ name,
218+ sc)
209219 } else {
210- format ! ( "{} `{}` should have a snake case name" ,
211- sort, name)
220+ format ! ( "{} `{}` should have a snake case name" , sort, name)
212221 } ;
213222 match span {
214223 Some ( span) => cx. span_lint ( NON_SNAKE_CASE , span, & msg) ,
@@ -226,31 +235,39 @@ impl LintPass for NonSnakeCase {
226235
227236impl LateLintPass for NonSnakeCase {
228237 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) ) ) ;
231242 if let Some ( ref name) = cx. tcx . sess . opts . crate_name {
232243 self . check_snake_case ( cx, "crate" , name, None ) ;
233244 } else if let Some ( ( attr, ref name) ) = attr_crate_name {
234245 self . check_snake_case ( cx, "crate" , name, Some ( attr. span ) ) ;
235246 }
236247 }
237248
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 ) {
241256 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+ }
251268 FnKind :: ItemFn ( name, ..) => {
252269 self . check_snake_case ( cx, "function" , & name. as_str ( ) , Some ( span) )
253- } ,
270+ }
254271 FnKind :: Closure ( _) => ( ) ,
255272 }
256273 }
@@ -263,13 +280,17 @@ impl LateLintPass for NonSnakeCase {
263280
264281 fn check_trait_item ( & mut self , cx : & LateContext , trait_item : & hir:: TraitItem ) {
265282 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 ( ) ,
267286 Some ( trait_item. span ) ) ;
268287 }
269288 }
270289
271290 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 ( ) ,
273294 Some ( t. lifetime . span ) ) ;
274295 }
275296
@@ -282,8 +303,12 @@ impl LateLintPass for NonSnakeCase {
282303 }
283304 }
284305
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 ) {
287312 for sf in s. fields ( ) {
288313 self . check_snake_case ( cx, "structure field" , & sf. name . as_str ( ) , Some ( sf. span ) ) ;
289314 }
@@ -306,13 +331,16 @@ impl NonUpperCaseGlobals {
306331 if s. chars ( ) . any ( |c| c. is_lowercase ( ) ) {
307332 let uc = NonSnakeCase :: to_snake_case ( & s) . to_uppercase ( ) ;
308333 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) ) ;
312340 } 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) ) ;
316344 }
317345 }
318346 }
@@ -341,8 +369,7 @@ impl LateLintPass for NonUpperCaseGlobals {
341369 fn check_trait_item ( & mut self , cx : & LateContext , ti : & hir:: TraitItem ) {
342370 match ti. node {
343371 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 ) ;
346373 }
347374 _ => { }
348375 }
@@ -351,8 +378,7 @@ impl LateLintPass for NonUpperCaseGlobals {
351378 fn check_impl_item ( & mut self , cx : & LateContext , ii : & hir:: ImplItem ) {
352379 match ii. node {
353380 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 ) ;
356382 }
357383 _ => { }
358384 }
@@ -363,8 +389,10 @@ impl LateLintPass for NonUpperCaseGlobals {
363389 if let PatKind :: Path ( None , ref path) = p. node {
364390 if !path. global && path. segments . len ( ) == 1 && path. segments [ 0 ] . parameters . is_empty ( ) {
365391 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 ) ;
368396 }
369397 }
370398 }
0 commit comments