@@ -910,9 +910,8 @@ impl NonSnakeCase {
910910 words. connect ( "_" )
911911 }
912912
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 {
916915 if ident. is_empty ( ) {
917916 return true ;
918917 }
@@ -931,18 +930,18 @@ impl NonSnakeCase {
931930 } )
932931 }
933932
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)
942938 } 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) ,
946945 }
947946 }
948947 }
@@ -953,47 +952,59 @@ impl LintPass for NonSnakeCase {
953952 lint_array ! ( NON_SNAKE_CASE )
954953 }
955954
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+
956965 fn check_fn ( & mut self , cx : & Context ,
957966 fk : visit:: FnKind , _: & ast:: FnDecl ,
958967 _: & ast:: Block , span : Span , id : ast:: NodeId ) {
959968 match fk {
960969 visit:: FkMethod ( ident, _, _) => match method_context ( cx, id, span) {
961970 MethodContext :: PlainImpl => {
962- self . check_snake_case ( cx, "method" , ident, span)
971+ self . check_snake_case ( cx, "method" , & token :: get_ident ( ident) , Some ( span) )
963972 } ,
964973 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) )
966975 } ,
967976 _ => ( ) ,
968977 } ,
969978 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) )
971980 } ,
972981 _ => ( ) ,
973982 }
974983 }
975984
976985 fn check_item ( & mut self , cx : & Context , it : & ast:: Item ) {
977986 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 ) ) ;
979988 }
980989 }
981990
982991 fn check_trait_item ( & mut self , cx : & Context , trait_item : & ast:: TraitItem ) {
983992 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 ) ) ;
985995 }
986996 }
987997
988998 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 ) ) ;
9901001 }
9911002
9921003 fn check_pat ( & mut self , cx : & Context , p : & ast:: Pat ) {
9931004 if let & ast:: PatIdent ( _, ref path1, _) = & p. node {
9941005 let def = cx. tcx . def_map . borrow ( ) . get ( & p. id ) . map ( |d| d. full_def ( ) ) ;
9951006 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 ) ) ;
9971008 }
9981009 }
9991010 }
@@ -1002,7 +1013,8 @@ impl LintPass for NonSnakeCase {
10021013 _: ast:: Ident , _: & ast:: Generics , _: ast:: NodeId ) {
10031014 for sf in & s. fields {
10041015 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 ) ) ;
10061018 }
10071019 }
10081020 }
0 commit comments