@@ -910,9 +910,8 @@ impl NonSnakeCase {
910
910
words. connect ( "_" )
911
911
}
912
912
913
- fn check_snake_case ( & self , cx : & Context , sort : & str , ident : ast:: Ident , span : Span ) {
914
- fn is_snake_case ( ident : ast:: Ident ) -> bool {
915
- let ident = token:: get_ident ( ident) ;
913
+ fn check_snake_case ( & self , cx : & Context , sort : & str , name : & str , span : Option < Span > ) {
914
+ fn is_snake_case ( ident : & str ) -> bool {
916
915
if ident. is_empty ( ) {
917
916
return true ;
918
917
}
@@ -931,18 +930,18 @@ impl NonSnakeCase {
931
930
} )
932
931
}
933
932
934
- let s = token:: get_ident ( ident) ;
935
-
936
- if !is_snake_case ( ident) {
937
- let sc = NonSnakeCase :: to_snake_case ( & s) ;
938
- if sc != & s[ ..] {
939
- cx. span_lint ( NON_SNAKE_CASE , span,
940
- & * format ! ( "{} `{}` should have a snake case name such as `{}`" ,
941
- sort, s, sc) ) ;
933
+ if !is_snake_case ( name) {
934
+ let sc = NonSnakeCase :: to_snake_case ( name) ;
935
+ let msg = if sc != name {
936
+ format ! ( "{} `{}` should have a snake case name such as `{}`" ,
937
+ sort, name, sc)
942
938
} else {
943
- cx. span_lint ( NON_SNAKE_CASE , span,
944
- & * format ! ( "{} `{}` should have a snake case name" ,
945
- sort, s) ) ;
939
+ format ! ( "{} `{}` should have a snake case name" ,
940
+ sort, name)
941
+ } ;
942
+ match span {
943
+ Some ( span) => cx. span_lint ( NON_SNAKE_CASE , span, & msg) ,
944
+ None => cx. lint ( NON_SNAKE_CASE , & msg) ,
946
945
}
947
946
}
948
947
}
@@ -953,47 +952,59 @@ impl LintPass for NonSnakeCase {
953
952
lint_array ! ( NON_SNAKE_CASE )
954
953
}
955
954
955
+ fn check_crate ( & mut self , cx : & Context , cr : & ast:: Crate ) {
956
+ let attr_crate_name = cr. attrs . iter ( ) . find ( |at| at. check_name ( "crate_name" ) )
957
+ . and_then ( |at| at. value_str ( ) . map ( |s| ( at, s) ) ) ;
958
+ if let Some ( ref name) = cx. tcx . sess . opts . crate_name {
959
+ self . check_snake_case ( cx, "crate" , name, None ) ;
960
+ } else if let Some ( ( attr, ref name) ) = attr_crate_name {
961
+ self . check_snake_case ( cx, "crate" , name, Some ( attr. span ) ) ;
962
+ }
963
+ }
964
+
956
965
fn check_fn ( & mut self , cx : & Context ,
957
966
fk : visit:: FnKind , _: & ast:: FnDecl ,
958
967
_: & ast:: Block , span : Span , id : ast:: NodeId ) {
959
968
match fk {
960
969
visit:: FkMethod ( ident, _, _) => match method_context ( cx, id, span) {
961
970
MethodContext :: PlainImpl => {
962
- self . check_snake_case ( cx, "method" , ident, span)
971
+ self . check_snake_case ( cx, "method" , & token :: get_ident ( ident) , Some ( span) )
963
972
} ,
964
973
MethodContext :: TraitDefaultImpl => {
965
- self . check_snake_case ( cx, "trait method" , ident, span)
974
+ self . check_snake_case ( cx, "trait method" , & token :: get_ident ( ident) , Some ( span) )
966
975
} ,
967
976
_ => ( ) ,
968
977
} ,
969
978
visit:: FkItemFn ( ident, _, _, _, _) => {
970
- self . check_snake_case ( cx, "function" , ident, span)
979
+ self . check_snake_case ( cx, "function" , & token :: get_ident ( ident) , Some ( span) )
971
980
} ,
972
981
_ => ( ) ,
973
982
}
974
983
}
975
984
976
985
fn check_item ( & mut self , cx : & Context , it : & ast:: Item ) {
977
986
if let ast:: ItemMod ( _) = it. node {
978
- self . check_snake_case ( cx, "module" , it. ident , it. span ) ;
987
+ self . check_snake_case ( cx, "module" , & token :: get_ident ( it. ident ) , Some ( it. span ) ) ;
979
988
}
980
989
}
981
990
982
991
fn check_trait_item ( & mut self , cx : & Context , trait_item : & ast:: TraitItem ) {
983
992
if let ast:: MethodTraitItem ( _, None ) = trait_item. node {
984
- self . check_snake_case ( cx, "trait method" , trait_item. ident , trait_item. span ) ;
993
+ self . check_snake_case ( cx, "trait method" , & token:: get_ident ( trait_item. ident ) ,
994
+ Some ( trait_item. span ) ) ;
985
995
}
986
996
}
987
997
988
998
fn check_lifetime_def ( & mut self , cx : & Context , t : & ast:: LifetimeDef ) {
989
- self . check_snake_case ( cx, "lifetime" , t. lifetime . name . ident ( ) , t. lifetime . span ) ;
999
+ self . check_snake_case ( cx, "lifetime" , & token:: get_ident ( t. lifetime . name . ident ( ) ) ,
1000
+ Some ( t. lifetime . span ) ) ;
990
1001
}
991
1002
992
1003
fn check_pat ( & mut self , cx : & Context , p : & ast:: Pat ) {
993
1004
if let & ast:: PatIdent ( _, ref path1, _) = & p. node {
994
1005
let def = cx. tcx . def_map . borrow ( ) . get ( & p. id ) . map ( |d| d. full_def ( ) ) ;
995
1006
if let Some ( def:: DefLocal ( _) ) = def {
996
- self . check_snake_case ( cx, "variable" , path1. node , p. span ) ;
1007
+ self . check_snake_case ( cx, "variable" , & token :: get_ident ( path1. node ) , Some ( p. span ) ) ;
997
1008
}
998
1009
}
999
1010
}
@@ -1002,7 +1013,8 @@ impl LintPass for NonSnakeCase {
1002
1013
_: ast:: Ident , _: & ast:: Generics , _: ast:: NodeId ) {
1003
1014
for sf in & s. fields {
1004
1015
if let ast:: StructField_ { kind : ast:: NamedField ( ident, _) , .. } = sf. node {
1005
- self . check_snake_case ( cx, "structure field" , ident, sf. span ) ;
1016
+ self . check_snake_case ( cx, "structure field" , & token:: get_ident ( ident) ,
1017
+ Some ( sf. span ) ) ;
1006
1018
}
1007
1019
}
1008
1020
}
0 commit comments