Skip to content

Commit 71ce559

Browse files
committed
Dead-code pass now marks and warns foreign items
1 parent d5ad32f commit 71ce559

File tree

5 files changed

+48
-13
lines changed

5 files changed

+48
-13
lines changed

src/librustc/middle/dead.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fn should_explore(tcx: ty::ctxt, def_id: ast::DefId) -> bool {
3737
match tcx.items.find(&def_id.node) {
3838
Some(&ast_map::node_item(..))
3939
| Some(&ast_map::node_method(..))
40+
| Some(&ast_map::node_foreign_item(..))
4041
| Some(&ast_map::node_trait_method(..)) => true,
4142
_ => false
4243
}
@@ -106,8 +107,7 @@ impl MarkSymbolVisitor {
106107
match item.node {
107108
ast::item_fn(..)
108109
| ast::item_ty(..)
109-
| ast::item_static(..)
110-
| ast::item_foreign_mod(_) => {
110+
| ast::item_static(..) => {
111111
visit::walk_item(self, item, ());
112112
}
113113
_ => ()
@@ -119,6 +119,9 @@ impl MarkSymbolVisitor {
119119
ast_map::node_method(method, _, _) => {
120120
visit::walk_block(self, method.body, ());
121121
}
122+
ast_map::node_foreign_item(foreign_item, _, _, _) => {
123+
visit::walk_foreign_item(self, foreign_item, ());
124+
}
122125
_ => ()
123126
}
124127
}
@@ -299,19 +302,31 @@ impl DeadVisitor {
299302
}
300303
false
301304
}
305+
306+
fn warn_dead_code(&mut self, id: ast::NodeId,
307+
span: codemap::Span, ident: &ast::Ident) {
308+
self.tcx.sess.add_lint(dead_code, id, span,
309+
format!("code is never used: `{}`",
310+
token::ident_to_str(ident)));
311+
}
302312
}
303313

304314
impl Visitor<()> for DeadVisitor {
305315
fn visit_item(&mut self, item: @ast::item, _: ()) {
306316
let ctor_id = get_struct_ctor_id(item);
307317
if !self.symbol_is_live(item.id, ctor_id) && should_warn(item) {
308-
self.tcx.sess.add_lint(dead_code, item.id, item.span,
309-
format!("code is never used: `{}`",
310-
token::ident_to_str(&item.ident)));
318+
self.warn_dead_code(item.id, item.span, &item.ident);
311319
}
312320
visit::walk_item(self, item, ());
313321
}
314322

323+
fn visit_foreign_item(&mut self, fi: @ast::foreign_item, _: ()) {
324+
if !self.symbol_is_live(fi.id, None) {
325+
self.warn_dead_code(fi.id, fi.span, &fi.ident);
326+
}
327+
visit::walk_foreign_item(self, fi, ());
328+
}
329+
315330
fn visit_fn(&mut self, fk: &visit::fn_kind,
316331
_: &ast::fn_decl, block: ast::P<ast::Block>,
317332
span: codemap::Span, id: ast::NodeId, _: ()) {
@@ -320,10 +335,7 @@ impl Visitor<()> for DeadVisitor {
320335
visit::fk_method(..) => {
321336
let ident = visit::name_of_fn(fk);
322337
if !self.symbol_is_live(id, None) {
323-
self.tcx.sess
324-
.add_lint(dead_code, id, span,
325-
format!("code is never used: `{}`",
326-
token::ident_to_str(&ident)));
338+
self.warn_dead_code(id, span, &ident);
327339
}
328340
}
329341
_ => ()

src/librustuv/uvll.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929

3030
#[allow(non_camel_case_types)]; // C types
3131

32-
use std::libc::{size_t, c_int, c_uint, c_void, c_char, uintptr_t, c_double};
32+
use std::libc::{size_t, c_int, c_uint, c_void, c_char, c_double};
3333
use std::libc::ssize_t;
3434
use std::libc::{malloc, free};
3535
use std::libc;
3636
use std::vec;
3737

38+
#[cfg(test)]
39+
use std::libc::uintptr_t;
40+
3841
pub use self::errors::*;
3942

4043
pub static OK: c_int = 0;
@@ -541,7 +544,9 @@ extern {
541544
pub fn rust_is_ipv4_sockaddr(addr: *sockaddr) -> c_int;
542545
pub fn rust_is_ipv6_sockaddr(addr: *sockaddr) -> c_int;
543546

547+
#[cfg(test)]
544548
fn rust_uv_handle_type_max() -> uintptr_t;
549+
#[cfg(test)]
545550
fn rust_uv_req_type_max() -> uintptr_t;
546551
fn rust_uv_get_udp_handle_from_send_req(req: *uv_udp_send_t) -> *uv_udp_t;
547552

src/libstd/num/cmath.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#[allow(missing_doc)];
1212
#[allow(non_uppercase_statics)];
13+
#[allow(dead_code)];
1314

1415
// function names are almost identical to C's libmath, a few have been
1516
// renamed, grep for "rename:"

src/test/compile-fail/lint-dead-code-3.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,27 @@ fn bar2() {
4040
pub fn pub_fn() {
4141
let foo2_struct = Foo2;
4242
foo2_struct.foo2();
43+
44+
blah::baz();
4345
}
4446

45-
// not warned because it's used in the parameter of `free` below
46-
enum c_void {}
47+
mod blah {
48+
use std::libc::size_t;
49+
// not warned because it's used in the parameter of `free` and return of
50+
// `malloc` below, which are also used.
51+
enum c_void {}
52+
53+
extern {
54+
fn free(p: *c_void);
55+
fn malloc(size: size_t) -> *c_void;
56+
}
57+
58+
pub fn baz() {
59+
unsafe { free(malloc(4)); }
60+
}
61+
}
4762

63+
enum c_void {} //~ ERROR: code is never used
4864
extern {
49-
fn free(p: *c_void);
65+
fn free(p: *c_void); //~ ERROR: code is never used
5066
}

src/test/compile-fail/warn-foreign-int-types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
#[forbid(ctypes)];
12+
#[allow(dead_code)];
1213

1314
mod xx {
1415
extern {

0 commit comments

Comments
 (0)