@@ -937,6 +937,34 @@ declare_lint! {
937
937
pub struct NonSnakeCase ;
938
938
939
939
impl NonSnakeCase {
940
+ fn to_snake_case ( mut str : & str ) -> String {
941
+ let mut words = vec ! [ ] ;
942
+ // Preserve leading underscores
943
+ str = str. trim_left_matches ( |& mut : c: char | {
944
+ if c == '_' {
945
+ words. push ( String :: new ( ) ) ;
946
+ true
947
+ } else { false }
948
+ } ) ;
949
+ for s in str. split ( '_' ) {
950
+ let mut last_upper = false ;
951
+ let mut buf = String :: new ( ) ;
952
+ if s. is_empty ( ) { continue ; }
953
+ for ch in s. chars ( ) {
954
+ if !buf. is_empty ( ) && buf != "'"
955
+ && ch. is_uppercase ( )
956
+ && !last_upper {
957
+ words. push ( buf) ;
958
+ buf = String :: new ( ) ;
959
+ }
960
+ last_upper = ch. is_uppercase ( ) ;
961
+ buf. push ( ch. to_lowercase ( ) ) ;
962
+ }
963
+ words. push ( buf) ;
964
+ }
965
+ words. connect ( "_" )
966
+ }
967
+
940
968
fn check_snake_case ( & self , cx : & Context , sort : & str , ident : ast:: Ident , span : Span ) {
941
969
fn is_snake_case ( ident : ast:: Ident ) -> bool {
942
970
let ident = token:: get_ident ( ident) ;
@@ -947,41 +975,28 @@ impl NonSnakeCase {
947
975
let mut allow_underscore = true ;
948
976
ident. chars ( ) . all ( |c| {
949
977
allow_underscore = match c {
950
- c if c. is_lowercase ( ) || c. is_numeric ( ) => true ,
951
- '_' if allow_underscore => false ,
978
+ '_' if !allow_underscore => return false ,
979
+ '_' => false ,
980
+ c if !c. is_uppercase ( ) => true ,
952
981
_ => return false ,
953
982
} ;
954
983
true
955
984
} )
956
985
}
957
986
958
- fn to_snake_case ( str : & str ) -> String {
959
- let mut words = vec ! [ ] ;
960
- for s in str. split ( '_' ) {
961
- let mut last_upper = false ;
962
- let mut buf = String :: new ( ) ;
963
- if s. is_empty ( ) { continue ; }
964
- for ch in s. chars ( ) {
965
- if !buf. is_empty ( ) && buf != "'"
966
- && ch. is_uppercase ( )
967
- && !last_upper {
968
- words. push ( buf) ;
969
- buf = String :: new ( ) ;
970
- }
971
- last_upper = ch. is_uppercase ( ) ;
972
- buf. push ( ch. to_lowercase ( ) ) ;
973
- }
974
- words. push ( buf) ;
975
- }
976
- words. connect ( "_" )
977
- }
978
-
979
987
let s = token:: get_ident ( ident) ;
980
988
981
989
if !is_snake_case ( ident) {
982
- cx. span_lint ( NON_SNAKE_CASE , span,
983
- & format ! ( "{} `{}` should have a snake case name such as `{}`" ,
984
- sort, s, to_snake_case( s. get( ) ) ) [ ] ) ;
990
+ let sc = NonSnakeCase :: to_snake_case ( s. get ( ) ) ;
991
+ if sc != s. get ( ) {
992
+ cx. span_lint ( NON_SNAKE_CASE , span,
993
+ & * format ! ( "{} `{}` should have a snake case name such as `{}`" ,
994
+ sort, s, sc) ) ;
995
+ } else {
996
+ cx. span_lint ( NON_SNAKE_CASE , span,
997
+ & * format ! ( "{} `{}` should have a snake case name" ,
998
+ sort, s) ) ;
999
+ }
985
1000
}
986
1001
}
987
1002
}
@@ -1049,6 +1064,26 @@ declare_lint! {
1049
1064
#[ derive( Copy ) ]
1050
1065
pub struct NonUpperCaseGlobals ;
1051
1066
1067
+ impl NonUpperCaseGlobals {
1068
+ fn check_upper_case ( cx : & Context , sort : & str , ident : ast:: Ident , span : Span ) {
1069
+ let s = token:: get_ident ( ident) ;
1070
+
1071
+ if s. get ( ) . chars ( ) . any ( |c| c. is_lowercase ( ) ) {
1072
+ let uc: String = NonSnakeCase :: to_snake_case ( s. get ( ) ) . chars ( )
1073
+ . map ( |c| c. to_uppercase ( ) ) . collect ( ) ;
1074
+ if uc != s. get ( ) {
1075
+ cx. span_lint ( NON_UPPER_CASE_GLOBALS , span,
1076
+ format ! ( "{} `{}` should have an upper case name such as `{}`" ,
1077
+ sort, s, uc) . as_slice ( ) ) ;
1078
+ } else {
1079
+ cx. span_lint ( NON_UPPER_CASE_GLOBALS , span,
1080
+ format ! ( "{} `{}` should have an upper case name" ,
1081
+ sort, s) . as_slice ( ) ) ;
1082
+ }
1083
+ }
1084
+ }
1085
+ }
1086
+
1052
1087
impl LintPass for NonUpperCaseGlobals {
1053
1088
fn get_lints ( & self ) -> LintArray {
1054
1089
lint_array ! ( NON_UPPER_CASE_GLOBALS )
@@ -1057,19 +1092,11 @@ impl LintPass for NonUpperCaseGlobals {
1057
1092
fn check_item ( & mut self , cx : & Context , it : & ast:: Item ) {
1058
1093
match it. node {
1059
1094
// only check static constants
1060
- ast:: ItemStatic ( _, ast:: MutImmutable , _) |
1095
+ ast:: ItemStatic ( _, ast:: MutImmutable , _) => {
1096
+ NonUpperCaseGlobals :: check_upper_case ( cx, "static constant" , it. ident , it. span ) ;
1097
+ }
1061
1098
ast:: ItemConst ( ..) => {
1062
- let s = token:: get_ident ( it. ident ) ;
1063
- // check for lowercase letters rather than non-uppercase
1064
- // ones (some scripts don't have a concept of
1065
- // upper/lowercase)
1066
- if s. get ( ) . chars ( ) . any ( |c| c. is_lowercase ( ) ) {
1067
- cx. span_lint ( NON_UPPER_CASE_GLOBALS , it. span ,
1068
- & format ! ( "static constant `{}` should have an uppercase name \
1069
- such as `{}`",
1070
- s. get( ) , & s. get( ) . chars( ) . map( |c| c. to_uppercase( ) )
1071
- . collect:: <String >( ) [ ] ) [ ] ) ;
1072
- }
1099
+ NonUpperCaseGlobals :: check_upper_case ( cx, "constant" , it. ident , it. span ) ;
1073
1100
}
1074
1101
_ => { }
1075
1102
}
@@ -1079,14 +1106,8 @@ impl LintPass for NonUpperCaseGlobals {
1079
1106
// Lint for constants that look like binding identifiers (#7526)
1080
1107
match ( & p. node , cx. tcx . def_map . borrow ( ) . get ( & p. id ) ) {
1081
1108
( & ast:: PatIdent ( _, ref path1, _) , Some ( & def:: DefConst ( ..) ) ) => {
1082
- let s = token:: get_ident ( path1. node ) ;
1083
- if s. get ( ) . chars ( ) . any ( |c| c. is_lowercase ( ) ) {
1084
- cx. span_lint ( NON_UPPER_CASE_GLOBALS , path1. span ,
1085
- & format ! ( "static constant in pattern `{}` should have an uppercase \
1086
- name such as `{}`",
1087
- s. get( ) , & s. get( ) . chars( ) . map( |c| c. to_uppercase( ) )
1088
- . collect:: <String >( ) [ ] ) [ ] ) ;
1089
- }
1109
+ NonUpperCaseGlobals :: check_upper_case ( cx, "constant in pattern" ,
1110
+ path1. node , p. span ) ;
1090
1111
}
1091
1112
_ => { }
1092
1113
}
0 commit comments