@@ -1253,12 +1253,21 @@ fn check_item_non_camel_case_types(cx: &Context, it: &ast::Item) {
12531253 !ident. char_at ( 0 ) . is_lowercase ( ) && !ident. contains_char ( '_' )
12541254 }
12551255
1256+ fn to_camel_case ( s : & str ) -> String {
1257+ s. split ( '_' ) . flat_map ( |word| word. chars ( ) . enumerate ( ) . map ( |( i, c) |
1258+ if i == 0 { c. to_uppercase ( ) }
1259+ else { c }
1260+ ) ) . collect ( )
1261+ }
1262+
12561263 fn check_case ( cx : & Context , sort : & str , ident : ast:: Ident , span : Span ) {
1264+ let s = token:: get_ident ( ident) ;
1265+
12571266 if !is_camel_case ( ident) {
12581267 cx. span_lint (
12591268 NonCamelCaseTypes , span,
1260- format ! ( "{} `{}` should have a camel case identifier " ,
1261- sort, token :: get_ident ( ident ) ) . as_slice ( ) ) ;
1269+ format ! ( "{} `{}` should have a camel case name such as `{}` " ,
1270+ sort, s , to_camel_case ( s . get ( ) ) ) . as_slice ( ) ) ;
12621271 }
12631272 }
12641273
@@ -1296,10 +1305,29 @@ fn check_snake_case(cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
12961305 } )
12971306 }
12981307
1308+ fn to_snake_case ( str : & str ) -> String {
1309+ let mut words = vec ! [ ] ;
1310+ for s in str. split ( '_' ) {
1311+ let mut buf = String :: new ( ) ;
1312+ if s. is_empty ( ) { continue ; }
1313+ for ch in s. chars ( ) {
1314+ if !buf. is_empty ( ) && ch. is_uppercase ( ) {
1315+ words. push ( buf) ;
1316+ buf = String :: new ( ) ;
1317+ }
1318+ buf. push_char ( ch. to_lowercase ( ) ) ;
1319+ }
1320+ words. push ( buf) ;
1321+ }
1322+ words. connect ( "_" )
1323+ }
1324+
1325+ let s = token:: get_ident ( ident) ;
1326+
12991327 if !is_snake_case ( ident) {
13001328 cx. span_lint ( NonSnakeCaseFunctions , span,
1301- format ! ( "{} `{}` should have a snake case identifier " ,
1302- sort, token :: get_ident ( ident ) ) . as_slice ( ) ) ;
1329+ format ! ( "{} `{}` should have a snake case name such as `{}` " ,
1330+ sort, s , to_snake_case ( s . get ( ) ) ) . as_slice ( ) ) ;
13031331 }
13041332}
13051333
@@ -1313,7 +1341,10 @@ fn check_item_non_uppercase_statics(cx: &Context, it: &ast::Item) {
13131341 // upper/lowercase)
13141342 if s. get ( ) . chars ( ) . any ( |c| c. is_lowercase ( ) ) {
13151343 cx. span_lint ( NonUppercaseStatics , it. span ,
1316- "static constant should have an uppercase identifier" ) ;
1344+ format ! ( "static constant `{}` should have an uppercase name \
1345+ such as `{}`", s. get( ) ,
1346+ s. get( ) . chars( ) . map( |c| c. to_uppercase( ) )
1347+ . collect:: <String >( ) . as_slice( ) ) . as_slice ( ) ) ;
13171348 }
13181349 }
13191350 _ => { }
@@ -1329,7 +1360,10 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
13291360 let s = token:: get_ident ( ident) ;
13301361 if s. get ( ) . chars ( ) . any ( |c| c. is_lowercase ( ) ) {
13311362 cx. span_lint ( NonUppercasePatternStatics , path. span ,
1332- "static constant in pattern should be all caps" ) ;
1363+ format ! ( "static constant in pattern `{}` should have an uppercase \
1364+ name such as `{}`", s. get( ) ,
1365+ s. get( ) . chars( ) . map( |c| c. to_uppercase( ) )
1366+ . collect:: <String >( ) . as_slice( ) ) . as_slice ( ) ) ;
13331367 }
13341368 }
13351369 _ => { }
0 commit comments