@@ -1326,12 +1326,21 @@ fn check_item_non_camel_case_types(cx: &Context, it: &ast::Item) {
13261326 !ident. char_at ( 0 ) . is_lowercase ( ) && !ident. contains_char ( '_' )
13271327 }
13281328
1329+ fn to_camel_case ( s : & str ) -> String {
1330+ s. split ( '_' ) . flat_map ( |word| word. chars ( ) . enumerate ( ) . map ( |( i, c) |
1331+ if i == 0 { c. to_uppercase ( ) }
1332+ else { c }
1333+ ) ) . collect ( )
1334+ }
1335+
13291336 fn check_case ( cx : & Context , sort : & str , ident : ast:: Ident , span : Span ) {
1337+ let s = token:: get_ident ( ident) ;
1338+
13301339 if !is_camel_case ( ident) {
13311340 cx. span_lint (
13321341 NonCamelCaseTypes , span,
1333- format ! ( "{} `{}` should have a camel case identifier " ,
1334- sort, token :: get_ident ( ident ) ) . as_slice ( ) ) ;
1342+ format ! ( "{} `{}` should have a camel case name such as `{}` " ,
1343+ sort, s , to_camel_case ( s . get ( ) ) ) . as_slice ( ) ) ;
13351344 }
13361345 }
13371346
@@ -1369,10 +1378,29 @@ fn check_snake_case(cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
13691378 } )
13701379 }
13711380
1381+ fn to_snake_case ( str : & str ) -> String {
1382+ let mut words = vec ! [ ] ;
1383+ for s in str. split ( '_' ) {
1384+ let mut buf = String :: new ( ) ;
1385+ if s. is_empty ( ) { continue ; }
1386+ for ch in s. chars ( ) {
1387+ if !buf. is_empty ( ) && ch. is_uppercase ( ) {
1388+ words. push ( buf) ;
1389+ buf = String :: new ( ) ;
1390+ }
1391+ buf. push_char ( ch. to_lowercase ( ) ) ;
1392+ }
1393+ words. push ( buf) ;
1394+ }
1395+ words. connect ( "_" )
1396+ }
1397+
1398+ let s = token:: get_ident ( ident) ;
1399+
13721400 if !is_snake_case ( ident) {
13731401 cx. span_lint ( NonSnakeCaseFunctions , span,
1374- format ! ( "{} `{}` should have a snake case identifier " ,
1375- sort, token :: get_ident ( ident ) ) . as_slice ( ) ) ;
1402+ format ! ( "{} `{}` should have a snake case name such as `{}` " ,
1403+ sort, s , to_snake_case ( s . get ( ) ) ) . as_slice ( ) ) ;
13761404 }
13771405}
13781406
@@ -1386,7 +1414,10 @@ fn check_item_non_uppercase_statics(cx: &Context, it: &ast::Item) {
13861414 // upper/lowercase)
13871415 if s. get ( ) . chars ( ) . any ( |c| c. is_lowercase ( ) ) {
13881416 cx. span_lint ( NonUppercaseStatics , it. span ,
1389- "static constant should have an uppercase identifier" ) ;
1417+ format ! ( "static constant `{}` should have an uppercase name \
1418+ such as `{}`", s. get( ) ,
1419+ s. get( ) . chars( ) . map( |c| c. to_uppercase( ) )
1420+ . collect:: <String >( ) . as_slice( ) ) . as_slice ( ) ) ;
13901421 }
13911422 }
13921423 _ => { }
@@ -1402,7 +1433,10 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
14021433 let s = token:: get_ident ( ident) ;
14031434 if s. get ( ) . chars ( ) . any ( |c| c. is_lowercase ( ) ) {
14041435 cx. span_lint ( NonUppercasePatternStatics , path. span ,
1405- "static constant in pattern should be all caps" ) ;
1436+ format ! ( "static constant in pattern `{}` should have an uppercase \
1437+ name such as `{}`", s. get( ) ,
1438+ s. get( ) . chars( ) . map( |c| c. to_uppercase( ) )
1439+ . collect:: <String >( ) . as_slice( ) ) . as_slice ( ) ) ;
14061440 }
14071441 }
14081442 _ => { }
0 commit comments