Skip to content

Commit a6310f6

Browse files
committed
auto merge of #10477 : ktt3ja/rust/dead-code, r=alexcrichton
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.
2 parents af6010c + 1755408 commit a6310f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+722
-1183
lines changed

src/compiletest/compiletest.rs

-4
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,6 @@ pub fn opt_str2(maybestr: Option<~str>) -> ~str {
192192
match maybestr { None => ~"(none)", Some(s) => { s } }
193193
}
194194

195-
pub fn str_opt(maybestr: ~str) -> Option<~str> {
196-
if maybestr != ~"(none)" { Some(maybestr) } else { None }
197-
}
198-
199195
pub fn str_mode(s: ~str) -> mode {
200196
match s {
201197
~"compile-fail" => mode_compile_fail,

src/compiletest/runtest.rs

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use header::TestProps;
1818
use header::load_props;
1919
use procsrv;
2020
use util::logv;
21+
#[cfg(target_os = "win32")]
2122
use util;
2223

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

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

823825
// Build the LD_LIBRARY_PATH variable as it would be seen on the command line
824826
// for diagnostic purposes
827+
#[cfg(target_os = "win32")]
825828
fn lib_path_cmd_prefix(path: &str) -> ~str {
826829
format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
827830
}

src/compiletest/util.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use common::config;
1212

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

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

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

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

46-
#[cfg(target_os = "linux")]
47-
#[cfg(target_os = "freebsd")]
48-
pub fn lib_path_env_var() -> ~str { ~"LD_LIBRARY_PATH" }
49-
50-
#[cfg(target_os = "macos")]
51-
pub fn lib_path_env_var() -> ~str { ~"DYLD_LIBRARY_PATH" }
52-
5348
#[cfg(target_os = "win32")]
5449
pub fn lib_path_env_var() -> ~str { ~"PATH" }
5550

56-
#[cfg(target_os = "linux")]
57-
#[cfg(target_os = "macos")]
58-
#[cfg(target_os = "freebsd")]
59-
pub fn path_div() -> ~str { ~":" }
60-
6151
#[cfg(target_os = "win32")]
6252
pub fn path_div() -> ~str { ~";" }
6353

src/etc/extract-tests.py

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#[ allow(dead_assignment) ];\n
6565
#[ allow(unused_mut) ];\n
6666
#[ allow(attribute_usage) ];\n
67+
#[ allow(dead_code) ];\n
6768
#[ feature(macro_rules, globs, struct_variant, managed_boxes) ];\n
6869
""" + block
6970
if xfail:

src/libextra/bitv.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,9 @@ impl Bitv {
382382
#[inline]
383383
pub fn negate(&mut self) {
384384
match self.rep {
385-
Small(ref mut b) => b.negate(),
386-
Big(ref mut s) => {
387-
s.each_storage(|w| { *w = !*w; true });
388-
}
389-
}
385+
Small(ref mut s) => s.negate(),
386+
Big(ref mut b) => b.negate(),
387+
}
390388
}
391389

392390
/**

0 commit comments

Comments
 (0)