Skip to content

Commit

Permalink
auto merge of #10477 : ktt3ja/rust/dead-code, r=alexcrichton
Browse files Browse the repository at this point in the history
PR for issue #1749 mainly to get some feedback and suggestion. This adds a pass that warns if a function, struct, enum, or static item is never used. For the following code,

```rust
pub static pub_static: int = 0;
static priv_static: int = 0;
static used_static: int = 0;

pub fn pub_fn() { used_fn(); }
fn priv_fn() { let unused_struct = PrivStruct; }
fn used_fn() {}

pub struct PubStruct();
struct PrivStruct();
struct UsedStruct1 { x: int }
struct UsedStruct2(int);
struct UsedStruct3();

pub enum pub_enum { foo1, bar1 }
enum priv_enum { foo2, bar2 }
enum used_enum { foo3, bar3 }

fn foo() {
	bar();
	let unused_enum = foo2;
}

fn bar() {
	foo();
}

fn main() {
	let used_struct1 = UsedStruct1 { x: 1 };
	let used_struct2 = UsedStruct2(1);
	let used_struct3 = UsedStruct3;
	let t = used_static;
	let e = foo3;
}
```

it would add the following warnings:

```rust
/home/ktt3ja/test.rs:2:0: 2:28 warning: code is never used: `priv_static`, #[warn(dead_code)] on by default
/home/ktt3ja/test.rs:2 static priv_static: int = 0;
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/ktt3ja/test.rs:6:0: 6:48 warning: code is never used: `priv_fn`, #[warn(dead_code)] on by default
/home/ktt3ja/test.rs:6 fn priv_fn() { let unused_struct = PrivStruct; }
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/ktt3ja/test.rs:10:0: 10:20 warning: code is never used: `PrivStruct`, #[warn(dead_code)] on by default
/home/ktt3ja/test.rs:10 struct PrivStruct();
                        ^~~~~~~~~~~~~~~~~~~~
/home/ktt3ja/test.rs:16:0: 16:29 warning: code is never used: `priv_enum`, #[warn(dead_code)] on by default
/home/ktt3ja/test.rs:16 enum priv_enum { foo2, bar2 }
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/ktt3ja/test.rs:19:0: 22:1 warning: code is never used: `foo`, #[warn(dead_code)] on by default
/home/ktt3ja/test.rs:19 fn foo() {
/home/ktt3ja/test.rs:20 	bar();
/home/ktt3ja/test.rs:21 	let unused_enum = foo2;
/home/ktt3ja/test.rs:22 }
/home/ktt3ja/test.rs:24:0: 26:1 warning: code is never used: `bar`, #[warn(dead_code)] on by default
/home/ktt3ja/test.rs:24 fn bar() {
/home/ktt3ja/test.rs:25 	foo();
/home/ktt3ja/test.rs:26 }
```

Furthermore, I would like to solicit some test cases since I haven't tested extensively and I'm still unclear about some of the things in here. For example, I'm not sure how reexports would affect this and just assumed that LiveContext (which is a copy of reachable::ReachableContext) does enough work to handle it. Also, the test case above doesn't include any impl or methods, etc.
  • Loading branch information
bors committed Dec 8, 2013
2 parents af6010c + 1755408 commit a6310f6
Show file tree
Hide file tree
Showing 100 changed files with 722 additions and 1,183 deletions.
4 changes: 0 additions & 4 deletions src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ pub fn opt_str2(maybestr: Option<~str>) -> ~str {
match maybestr { None => ~"(none)", Some(s) => { s } }
}

pub fn str_opt(maybestr: ~str) -> Option<~str> {
if maybestr != ~"(none)" { Some(maybestr) } else { None }
}

pub fn str_mode(s: ~str) -> mode {
match s {
~"compile-fail" => mode_compile_fail,
Expand Down
3 changes: 3 additions & 0 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use header::TestProps;
use header::load_props;
use procsrv;
use util::logv;
#[cfg(target_os = "win32")]
use util;

use std::io::File;
Expand Down Expand Up @@ -482,6 +483,7 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
format!("{}:{}:", testfile.display(), ee.line)
}).collect::<~[~str]>();

#[cfg(target_os = "win32")]
fn to_lower( s : &str ) -> ~str {
let i = s.chars();
let c : ~[char] = i.map( |c| {
Expand Down Expand Up @@ -822,6 +824,7 @@ fn make_cmdline(libpath: &str, prog: &str, args: &[~str]) -> ~str {

// Build the LD_LIBRARY_PATH variable as it would be seen on the command line
// for diagnostic purposes
#[cfg(target_os = "win32")]
fn lib_path_cmd_prefix(path: &str) -> ~str {
format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
}
Expand Down
14 changes: 2 additions & 12 deletions src/compiletest/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use common::config;

#[cfg(target_os = "win32")]
use std::os::getenv;

/// Conversion table from triple OS name to Rust SYSNAME
Expand All @@ -31,6 +32,7 @@ pub fn get_os(triple: &str) -> &'static str {
fail!("Cannot determine OS from triple");
}

#[cfg(target_os = "win32")]
pub fn make_new_path(path: &str) -> ~str {

// Windows just uses PATH as the library search path, so we have to
Expand All @@ -43,21 +45,9 @@ pub fn make_new_path(path: &str) -> ~str {
}
}

#[cfg(target_os = "linux")]
#[cfg(target_os = "freebsd")]
pub fn lib_path_env_var() -> ~str { ~"LD_LIBRARY_PATH" }

#[cfg(target_os = "macos")]
pub fn lib_path_env_var() -> ~str { ~"DYLD_LIBRARY_PATH" }

#[cfg(target_os = "win32")]
pub fn lib_path_env_var() -> ~str { ~"PATH" }

#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
pub fn path_div() -> ~str { ~":" }

#[cfg(target_os = "win32")]
pub fn path_div() -> ~str { ~";" }
Expand Down
1 change: 1 addition & 0 deletions src/etc/extract-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#[ allow(dead_assignment) ];\n
#[ allow(unused_mut) ];\n
#[ allow(attribute_usage) ];\n
#[ allow(dead_code) ];\n
#[ feature(macro_rules, globs, struct_variant, managed_boxes) ];\n
""" + block
if xfail:
Expand Down
8 changes: 3 additions & 5 deletions src/libextra/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,9 @@ impl Bitv {
#[inline]
pub fn negate(&mut self) {
match self.rep {
Small(ref mut b) => b.negate(),
Big(ref mut s) => {
s.each_storage(|w| { *w = !*w; true });
}
}
Small(ref mut s) => s.negate(),
Big(ref mut b) => b.negate(),
}
}

/**
Expand Down
Loading

0 comments on commit a6310f6

Please sign in to comment.