about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKiet Tran <ktt3ja@gmail.com>2013-12-14 00:08:26 -0500
committerKiet Tran <ktt3ja@gmail.com>2013-12-14 00:35:41 -0500
commit71ce559f7de6b50dde7af7cf22ff6c4b2942d3c1 (patch)
tree153c00fed7769fe8f1881ee28f9a395d471a1168
parentd5ad32f388557c6b1f709d59a940adfa3332a5f2 (diff)
downloadrust-71ce559f7de6b50dde7af7cf22ff6c4b2942d3c1.tar.gz
rust-71ce559f7de6b50dde7af7cf22ff6c4b2942d3c1.zip
Dead-code pass now marks and warns foreign items
-rw-r--r--src/librustc/middle/dead.rs30
-rw-r--r--src/librustuv/uvll.rs7
-rw-r--r--src/libstd/num/cmath.rs1
-rw-r--r--src/test/compile-fail/lint-dead-code-3.rs22
-rw-r--r--src/test/compile-fail/warn-foreign-int-types.rs1
5 files changed, 48 insertions, 13 deletions
diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs
index a4e2c9742c8..9007e4bd90a 100644
--- a/src/librustc/middle/dead.rs
+++ b/src/librustc/middle/dead.rs
@@ -37,6 +37,7 @@ fn should_explore(tcx: ty::ctxt, def_id: ast::DefId) -> bool {
     match tcx.items.find(&def_id.node) {
         Some(&ast_map::node_item(..))
         | Some(&ast_map::node_method(..))
+        | Some(&ast_map::node_foreign_item(..))
         | Some(&ast_map::node_trait_method(..)) => true,
         _ => false
     }
@@ -106,8 +107,7 @@ impl MarkSymbolVisitor {
                 match item.node {
                     ast::item_fn(..)
                     | ast::item_ty(..)
-                    | ast::item_static(..)
-                    | ast::item_foreign_mod(_) => {
+                    | ast::item_static(..) => {
                         visit::walk_item(self, item, ());
                     }
                     _ => ()
@@ -119,6 +119,9 @@ impl MarkSymbolVisitor {
             ast_map::node_method(method, _, _) => {
                 visit::walk_block(self, method.body, ());
             }
+            ast_map::node_foreign_item(foreign_item, _, _, _) => {
+                visit::walk_foreign_item(self, foreign_item, ());
+            }
             _ => ()
         }
     }
@@ -299,19 +302,31 @@ impl DeadVisitor {
         }
         false
     }
+
+    fn warn_dead_code(&mut self, id: ast::NodeId,
+                      span: codemap::Span, ident: &ast::Ident) {
+        self.tcx.sess.add_lint(dead_code, id, span,
+                               format!("code is never used: `{}`",
+                                       token::ident_to_str(ident)));
+    }
 }
 
 impl Visitor<()> for DeadVisitor {
     fn visit_item(&mut self, item: @ast::item, _: ()) {
         let ctor_id = get_struct_ctor_id(item);
         if !self.symbol_is_live(item.id, ctor_id) && should_warn(item) {
-            self.tcx.sess.add_lint(dead_code, item.id, item.span,
-                                   format!("code is never used: `{}`",
-                                           token::ident_to_str(&item.ident)));
+            self.warn_dead_code(item.id, item.span, &item.ident);
         }
         visit::walk_item(self, item, ());
     }
 
+    fn visit_foreign_item(&mut self, fi: @ast::foreign_item, _: ()) {
+        if !self.symbol_is_live(fi.id, None) {
+            self.warn_dead_code(fi.id, fi.span, &fi.ident);
+        }
+        visit::walk_foreign_item(self, fi, ());
+    }
+
     fn visit_fn(&mut self, fk: &visit::fn_kind,
                 _: &ast::fn_decl, block: ast::P<ast::Block>,
                 span: codemap::Span, id: ast::NodeId, _: ()) {
@@ -320,10 +335,7 @@ impl Visitor<()> for DeadVisitor {
             visit::fk_method(..) => {
                 let ident = visit::name_of_fn(fk);
                 if !self.symbol_is_live(id, None) {
-                    self.tcx.sess
-                            .add_lint(dead_code, id, span,
-                                      format!("code is never used: `{}`",
-                                              token::ident_to_str(&ident)));
+                    self.warn_dead_code(id, span, &ident);
                 }
             }
             _ => ()
diff --git a/src/librustuv/uvll.rs b/src/librustuv/uvll.rs
index 09ebd29bc22..c1d4d367e25 100644
--- a/src/librustuv/uvll.rs
+++ b/src/librustuv/uvll.rs
@@ -29,12 +29,15 @@
 
 #[allow(non_camel_case_types)]; // C types
 
-use std::libc::{size_t, c_int, c_uint, c_void, c_char, uintptr_t, c_double};
+use std::libc::{size_t, c_int, c_uint, c_void, c_char, c_double};
 use std::libc::ssize_t;
 use std::libc::{malloc, free};
 use std::libc;
 use std::vec;
 
+#[cfg(test)]
+use std::libc::uintptr_t;
+
 pub use self::errors::*;
 
 pub static OK: c_int = 0;
@@ -541,7 +544,9 @@ extern {
     pub fn rust_is_ipv4_sockaddr(addr: *sockaddr) -> c_int;
     pub fn rust_is_ipv6_sockaddr(addr: *sockaddr) -> c_int;
 
+    #[cfg(test)]
     fn rust_uv_handle_type_max() -> uintptr_t;
+    #[cfg(test)]
     fn rust_uv_req_type_max() -> uintptr_t;
     fn rust_uv_get_udp_handle_from_send_req(req: *uv_udp_send_t) -> *uv_udp_t;
 
diff --git a/src/libstd/num/cmath.rs b/src/libstd/num/cmath.rs
index 72051a93398..5212b6fc15e 100644
--- a/src/libstd/num/cmath.rs
+++ b/src/libstd/num/cmath.rs
@@ -10,6 +10,7 @@
 
 #[allow(missing_doc)];
 #[allow(non_uppercase_statics)];
+#[allow(dead_code)];
 
 // function names are almost identical to C's libmath, a few have been
 // renamed, grep for "rename:"
diff --git a/src/test/compile-fail/lint-dead-code-3.rs b/src/test/compile-fail/lint-dead-code-3.rs
index 8a5f239ed05..29d77959997 100644
--- a/src/test/compile-fail/lint-dead-code-3.rs
+++ b/src/test/compile-fail/lint-dead-code-3.rs
@@ -40,11 +40,27 @@ fn bar2() {
 pub fn pub_fn() {
     let foo2_struct = Foo2;
     foo2_struct.foo2();
+
+    blah::baz();
 }
 
-// not warned because it's used in the parameter of `free` below
-enum c_void {}
+mod blah {
+    use std::libc::size_t;
+    // not warned because it's used in the parameter of `free` and return of
+    // `malloc` below, which are also used.
+    enum c_void {}
+
+    extern {
+        fn free(p: *c_void);
+        fn malloc(size: size_t) -> *c_void;
+    }
+
+    pub fn baz() {
+        unsafe { free(malloc(4)); }
+    }
+}
 
+enum c_void {} //~ ERROR: code is never used
 extern {
-    fn free(p: *c_void);
+    fn free(p: *c_void); //~ ERROR: code is never used
 }
diff --git a/src/test/compile-fail/warn-foreign-int-types.rs b/src/test/compile-fail/warn-foreign-int-types.rs
index be6871ae6ff..726d778c3bb 100644
--- a/src/test/compile-fail/warn-foreign-int-types.rs
+++ b/src/test/compile-fail/warn-foreign-int-types.rs
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #[forbid(ctypes)];
+#[allow(dead_code)];
 
 mod xx {
     extern {