Skip to content

Commit aed53f9

Browse files
committed
Promote unreachable code to being a lint attribute
1 parent 237dce1 commit aed53f9

File tree

9 files changed

+37
-21
lines changed

9 files changed

+37
-21
lines changed

src/librustc/middle/lint.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub enum lint {
9696
unnecessary_allocation,
9797

9898
missing_doc,
99+
unreachable_code,
99100
}
100101

101102
pub fn level_to_str(lv: level) -> &'static str {
@@ -273,6 +274,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
273274
desc: "detects missing documentation for public members",
274275
default: allow
275276
}),
277+
278+
("unreachable_code",
279+
LintSpec {
280+
lint: unreachable_code,
281+
desc: "detects unreachable code",
282+
default: warn
283+
}),
276284
];
277285

278286
/*

src/librustc/middle/typeck/check/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ use core::prelude::*;
8181
use middle::const_eval;
8282
use middle::pat_util::pat_id_map;
8383
use middle::pat_util;
84+
use middle::lint::unreachable_code;
8485
use middle::ty::{FnSig, VariantInfo_};
8586
use middle::ty::{ty_param_bounds_and_ty, ty_param_substs_and_ty};
8687
use middle::ty::{substs, param_ty};
@@ -2937,7 +2938,8 @@ pub fn check_block_with_expected(fcx: @mut FnCtxt,
29372938
let mut any_err = false;
29382939
for blk.node.stmts.each |s| {
29392940
check_stmt(fcx, *s);
2940-
let s_ty = fcx.node_ty(ast_util::stmt_id(*s));
2941+
let s_id = ast_util::stmt_id(*s);
2942+
let s_ty = fcx.node_ty(s_id);
29412943
if last_was_bot && !warned && match s.node {
29422944
ast::stmt_decl(@codemap::spanned { node: ast::decl_local(_),
29432945
_}, _) |
@@ -2946,7 +2948,8 @@ pub fn check_block_with_expected(fcx: @mut FnCtxt,
29462948
}
29472949
_ => false
29482950
} {
2949-
fcx.ccx.tcx.sess.span_warn(s.span, "unreachable statement");
2951+
fcx.ccx.tcx.sess.add_lint(unreachable_code, s_id, s.span,
2952+
~"unreachable statement");
29502953
warned = true;
29512954
}
29522955
if ty::type_is_bot(s_ty) {

src/test/compile-fail/dead-code-ret.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
// option. This file may not be copied, modified, or distributed
1010
// except according to those terms.
1111

12-
13-
fn f(caller: &str) {
14-
debug!(caller);
15-
let x: uint = 0u32; // induce type error //~ ERROR mismatched types
16-
}
12+
#[deny(unreachable_code)];
1713

1814
fn main() {
19-
return f("main");
20-
debug!("Paul is dead"); //~ WARNING unreachable
15+
return;
16+
debug!("Paul is dead"); //~ ERROR: unreachable
2117
}

src/test/compile-fail/issue-2150.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[deny(unreachable_code)];
12+
#[allow(unused_variable)];
13+
1114
fn fail_len(v: ~[int]) -> uint {
12-
let mut i = fail!();
15+
let mut i = 3;
16+
fail!();
1317
for v.each |x| { i += 1u; }
14-
//~^ WARNING unreachable statement
15-
//~^^ ERROR the type of this value must be known
18+
//~^ ERROR: unreachable statement
1619
return i;
1720
}
1821
fn main() {}

src/test/compile-fail/issue-897-2.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[deny(unreachable_code)];
12+
1113
fn g() -> ! { fail!(); }
1214
fn f() -> ! {
13-
return 42i; //~ ERROR expected `!` but found `int`
14-
g(); //~ WARNING unreachable statement
15+
return g();
16+
g(); //~ ERROR: unreachable statement
1517
}
1618
fn main() { }

src/test/compile-fail/issue-897.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[deny(unreachable_code)];
12+
1113
fn f() -> ! {
12-
return 42i; //~ ERROR expected `!` but found `int`
13-
fail!(); //~ WARNING unreachable statement
14+
return fail!();
15+
fail!(); //~ ERROR: unreachable statement
1416
}
1517
fn main() { }

src/test/compile-fail/liveness-break-uninit-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn foo() -> int {
1313

1414
while 1 != 2 {
1515
break;
16-
x = 0; //~ WARNING unreachable statement
16+
x = 0;
1717
}
1818

1919
debug!(x); //~ ERROR use of possibly uninitialized variable: `x`

src/test/compile-fail/liveness-break-uninit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn foo() -> int {
1313

1414
loop {
1515
break;
16-
x = 0; //~ WARNING unreachable statement
16+
x = 0;
1717
}
1818

1919
debug!(x); //~ ERROR use of possibly uninitialized variable: `x`

src/test/compile-fail/unreachable-code.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:unreachable statement
11+
#[deny(unreachable_code)];
12+
#[allow(unused_variable)];
13+
1214
fn main() {
1315
loop{}
14-
// red herring to make sure compilation fails
15-
error!(42 == 'c');
16+
17+
let a = 3; //~ ERROR: unreachable statement
1618
}

0 commit comments

Comments
 (0)