about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2013-05-30 03:03:32 -0500
committerAlex Crichton <alex@alexcrichton.com>2013-05-30 20:45:13 -0500
commitaed53f9bf0eed5526891d3998d2a570840e453f7 (patch)
tree8edabe8dc9cf03ade8b383e590927b1763e59d36
parent237dce12c934287abd0bbe5a35309b1dc4f0d2c0 (diff)
downloadrust-aed53f9bf0eed5526891d3998d2a570840e453f7.tar.gz
rust-aed53f9bf0eed5526891d3998d2a570840e453f7.zip
Promote unreachable code to being a lint attribute
-rw-r--r--src/librustc/middle/lint.rs8
-rw-r--r--src/librustc/middle/typeck/check/mod.rs7
-rw-r--r--src/test/compile-fail/dead-code-ret.rs10
-rw-r--r--src/test/compile-fail/issue-2150.rs9
-rw-r--r--src/test/compile-fail/issue-897-2.rs6
-rw-r--r--src/test/compile-fail/issue-897.rs6
-rw-r--r--src/test/compile-fail/liveness-break-uninit-2.rs2
-rw-r--r--src/test/compile-fail/liveness-break-uninit.rs2
-rw-r--r--src/test/compile-fail/unreachable-code.rs8
9 files changed, 37 insertions, 21 deletions
diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs
index 6dd911e8ef3..c460ec89e4e 100644
--- a/src/librustc/middle/lint.rs
+++ b/src/librustc/middle/lint.rs
@@ -96,6 +96,7 @@ pub enum lint {
     unnecessary_allocation,
 
     missing_doc,
+    unreachable_code,
 }
 
 pub fn level_to_str(lv: level) -> &'static str {
@@ -273,6 +274,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
         desc: "detects missing documentation for public members",
         default: allow
     }),
+
+    ("unreachable_code",
+     LintSpec {
+        lint: unreachable_code,
+        desc: "detects unreachable code",
+        default: warn
+    }),
 ];
 
 /*
diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs
index 61da263e843..f8481d4cf90 100644
--- a/src/librustc/middle/typeck/check/mod.rs
+++ b/src/librustc/middle/typeck/check/mod.rs
@@ -81,6 +81,7 @@ use core::prelude::*;
 use middle::const_eval;
 use middle::pat_util::pat_id_map;
 use middle::pat_util;
+use middle::lint::unreachable_code;
 use middle::ty::{FnSig, VariantInfo_};
 use middle::ty::{ty_param_bounds_and_ty, ty_param_substs_and_ty};
 use middle::ty::{substs, param_ty};
@@ -2937,7 +2938,8 @@ pub fn check_block_with_expected(fcx: @mut FnCtxt,
         let mut any_err = false;
         for blk.node.stmts.each |s| {
             check_stmt(fcx, *s);
-            let s_ty = fcx.node_ty(ast_util::stmt_id(*s));
+            let s_id = ast_util::stmt_id(*s);
+            let s_ty = fcx.node_ty(s_id);
             if last_was_bot && !warned && match s.node {
                   ast::stmt_decl(@codemap::spanned { node: ast::decl_local(_),
                                                  _}, _) |
@@ -2946,7 +2948,8 @@ pub fn check_block_with_expected(fcx: @mut FnCtxt,
                   }
                   _ => false
                 } {
-                fcx.ccx.tcx.sess.span_warn(s.span, "unreachable statement");
+                fcx.ccx.tcx.sess.add_lint(unreachable_code, s_id, s.span,
+                                          ~"unreachable statement");
                 warned = true;
             }
             if ty::type_is_bot(s_ty) {
diff --git a/src/test/compile-fail/dead-code-ret.rs b/src/test/compile-fail/dead-code-ret.rs
index 5fa796db884..91b89a67ee3 100644
--- a/src/test/compile-fail/dead-code-ret.rs
+++ b/src/test/compile-fail/dead-code-ret.rs
@@ -9,13 +9,9 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-
-fn f(caller: &str) {
-    debug!(caller);
-    let x: uint = 0u32; // induce type error //~ ERROR mismatched types
-}
+#[deny(unreachable_code)];
 
 fn main() {
-    return f("main");
-    debug!("Paul is dead"); //~ WARNING unreachable
+    return;
+    debug!("Paul is dead"); //~ ERROR: unreachable
 }
diff --git a/src/test/compile-fail/issue-2150.rs b/src/test/compile-fail/issue-2150.rs
index 9f2f9a855ed..0b35104841e 100644
--- a/src/test/compile-fail/issue-2150.rs
+++ b/src/test/compile-fail/issue-2150.rs
@@ -8,11 +8,14 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#[deny(unreachable_code)];
+#[allow(unused_variable)];
+
 fn fail_len(v: ~[int]) -> uint {
-    let mut i = fail!();
+    let mut i = 3;
+    fail!();
     for v.each |x| { i += 1u; }
-    //~^ WARNING unreachable statement
-    //~^^ ERROR the type of this value must be known
+    //~^ ERROR: unreachable statement
     return i;
 }
 fn main() {}
diff --git a/src/test/compile-fail/issue-897-2.rs b/src/test/compile-fail/issue-897-2.rs
index 253563c1219..eb60e34df8f 100644
--- a/src/test/compile-fail/issue-897-2.rs
+++ b/src/test/compile-fail/issue-897-2.rs
@@ -8,9 +8,11 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#[deny(unreachable_code)];
+
 fn g() -> ! { fail!(); }
 fn f() -> ! {
-    return 42i; //~ ERROR expected `!` but found `int`
-    g(); //~ WARNING unreachable statement
+    return g();
+    g(); //~ ERROR: unreachable statement
 }
 fn main() { }
diff --git a/src/test/compile-fail/issue-897.rs b/src/test/compile-fail/issue-897.rs
index 503574fce87..103156175a3 100644
--- a/src/test/compile-fail/issue-897.rs
+++ b/src/test/compile-fail/issue-897.rs
@@ -8,8 +8,10 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#[deny(unreachable_code)];
+
 fn f() -> ! {
-    return 42i; //~ ERROR expected `!` but found `int`
-    fail!(); //~ WARNING unreachable statement
+    return fail!();
+    fail!(); //~ ERROR: unreachable statement
 }
 fn main() { }
diff --git a/src/test/compile-fail/liveness-break-uninit-2.rs b/src/test/compile-fail/liveness-break-uninit-2.rs
index c87439db617..2ed02e2cdd7 100644
--- a/src/test/compile-fail/liveness-break-uninit-2.rs
+++ b/src/test/compile-fail/liveness-break-uninit-2.rs
@@ -13,7 +13,7 @@ fn foo() -> int {
 
     while 1 != 2  {
         break;
-        x = 0; //~ WARNING unreachable statement
+        x = 0;
     }
 
     debug!(x); //~ ERROR use of possibly uninitialized variable: `x`
diff --git a/src/test/compile-fail/liveness-break-uninit.rs b/src/test/compile-fail/liveness-break-uninit.rs
index 07075e4ef63..2dcbad2804c 100644
--- a/src/test/compile-fail/liveness-break-uninit.rs
+++ b/src/test/compile-fail/liveness-break-uninit.rs
@@ -13,7 +13,7 @@ fn foo() -> int {
 
     loop {
         break;
-        x = 0;  //~ WARNING unreachable statement
+        x = 0;
     }
 
     debug!(x); //~ ERROR use of possibly uninitialized variable: `x`
diff --git a/src/test/compile-fail/unreachable-code.rs b/src/test/compile-fail/unreachable-code.rs
index f1fbc5b009e..a9365eeda1c 100644
--- a/src/test/compile-fail/unreachable-code.rs
+++ b/src/test/compile-fail/unreachable-code.rs
@@ -8,9 +8,11 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// error-pattern:unreachable statement
+#[deny(unreachable_code)];
+#[allow(unused_variable)];
+
 fn main() {
   loop{}
-             // red herring to make sure compilation fails
-  error!(42 == 'c');
+
+  let a = 3; //~ ERROR: unreachable statement
 }