@@ -115,6 +115,8 @@ pub enum Lint {
115115 DeprecatedOwnedVector ,
116116
117117 Warnings ,
118+
119+ RawPointerDeriving ,
118120}
119121
120122pub fn level_to_str ( lv : level ) -> & ' static str {
@@ -406,6 +408,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
406408 desc : "use of a `~[T]` vector" ,
407409 default : allow,
408410 } ) ,
411+
412+ ( "raw_pointer_deriving" ,
413+ LintSpec {
414+ lint : RawPointerDeriving ,
415+ desc : "uses of #[deriving] with raw pointers are rarely correct" ,
416+ default : warn,
417+ } ) ,
409418] ;
410419
411420/*
@@ -959,6 +968,37 @@ fn check_heap_item(cx: &Context, it: &ast::Item) {
959968 }
960969}
961970
971+ struct RawPtrDerivingVisitor < ' a > {
972+ cx : & ' a Context < ' a >
973+ }
974+
975+ impl < ' a > Visitor < ( ) > for RawPtrDerivingVisitor < ' a > {
976+ fn visit_ty ( & mut self , ty : & ast:: Ty , _: ( ) ) {
977+ static MSG : & ' static str = "use of `#[deriving]` with a raw pointer" ;
978+ match ty. node {
979+ ast:: TyPtr ( ..) => self . cx . span_lint ( RawPointerDeriving , ty. span , MSG ) ,
980+ _ => { }
981+ }
982+ visit:: walk_ty ( self , ty, ( ) ) ;
983+ }
984+ // explicit override to a no-op to reduce code bloat
985+ fn visit_expr ( & mut self , _: & ast:: Expr , _: ( ) ) { }
986+ fn visit_block ( & mut self , _: & ast:: Block , _: ( ) ) { }
987+ }
988+
989+ fn check_raw_ptr_deriving ( cx : & Context , item : & ast:: Item ) {
990+ if !attr:: contains_name ( item. attrs . as_slice ( ) , "deriving" ) {
991+ return
992+ }
993+ match item. node {
994+ ast:: ItemStruct ( ..) | ast:: ItemEnum ( ..) => {
995+ let mut visitor = RawPtrDerivingVisitor { cx : cx } ;
996+ visit:: walk_item ( & mut visitor, item, ( ) ) ;
997+ }
998+ _ => { }
999+ }
1000+ }
1001+
9621002static crate_attrs: & ' static [ & ' static str ] = & [
9631003 "crate_type" , "feature" , "no_start" , "no_main" , "no_std" , "crate_id" ,
9641004 "desc" , "comment" , "license" , "copyright" , // not used in rustc now
@@ -1585,6 +1625,7 @@ impl<'a> Visitor<()> for Context<'a> {
15851625 check_heap_item ( cx, it) ;
15861626 check_missing_doc_item ( cx, it) ;
15871627 check_attrs_usage ( cx, it. attrs . as_slice ( ) ) ;
1628+ check_raw_ptr_deriving ( cx, it) ;
15881629
15891630 cx. visit_ids ( |v| v. visit_item ( it, ( ) ) ) ;
15901631
0 commit comments