Skip to content

Commit

Permalink
Dead-code pass now marks and warns foreign items
Browse files Browse the repository at this point in the history
  • Loading branch information
ktt3ja committed Dec 14, 2013
1 parent d5ad32f commit 71ce559
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
30 changes: 21 additions & 9 deletions src/librustc/middle/dead.rs
Expand Up @@ -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
}
Expand Down Expand Up @@ -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, ());
}
_ => ()
Expand All @@ -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, ());
}
_ => ()
}
}
Expand Down Expand Up @@ -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, _: ()) {
Expand All @@ -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);
}
}
_ => ()
Expand Down
7 changes: 6 additions & 1 deletion src/librustuv/uvll.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/libstd/num/cmath.rs
Expand Up @@ -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:"
Expand Down
22 changes: 19 additions & 3 deletions src/test/compile-fail/lint-dead-code-3.rs
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions src/test/compile-fail/warn-foreign-int-types.rs
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

#[forbid(ctypes)];
#[allow(dead_code)];

mod xx {
extern {
Expand Down

4 comments on commit 71ce559

@bors
Copy link
Contributor

@bors bors commented on 71ce559 Dec 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at ktt3ja@71ce559

@bors
Copy link
Contributor

@bors bors commented on 71ce559 Dec 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging ktt3ja/rust/issue-10865 = 71ce559 into auto

@bors
Copy link
Contributor

@bors bors commented on 71ce559 Dec 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ktt3ja/rust/issue-10865 = 71ce559 merged ok, testing candidate = ccc5f52b

Please sign in to comment.