Skip to content

Commit

Permalink
Add check for path-statements, close #400.
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Apr 26, 2012
1 parent 33a296f commit 1f92538
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/rustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ at all.
enum lint {
ctypes,
unused_imports,
while_true
while_true,
path_statement,
}

enum level {
Expand Down Expand Up @@ -56,7 +57,13 @@ fn get_lint_dict() -> lint_dict {
("while_true",
@{lint: while_true,
desc: "suggest using loop { } instead of while(true) { }",
default: warn}),

("path_statement",
@{lint: path_statement,
desc: "path statements with no effect",
default: warn})

];
hash_from_strs(v)
}
Expand Down Expand Up @@ -177,6 +184,7 @@ fn check_item(cx: ctxt, i: @ast::item) {
ctypes { check_item_ctypes(cx, level, i); }
unused_imports { check_item_unused_imports(cx, level, i); }
while_true { check_item_while_true(cx, level, i); }
path_statement { check_item_path_statement(cx, level, i); }
}
}
}
Expand Down Expand Up @@ -252,6 +260,25 @@ fn check_item_ctypes(cx: ctxt, level: level, it: @ast::item) {
}
}

fn check_item_path_statement(cx: ctxt, level: level, it: @ast::item) {
let visit = visit::mk_simple_visitor(@{
visit_stmt: fn@(s: @ast::stmt) {
alt s.node {
ast::stmt_semi(@{id: _,
node: ast::expr_path(@path),
span: _}, _) {
cx.span_lint(
level, s.span,
"path statement with no effect");
}
_ {}
}
}
with *visit::default_simple_visitor()
});
visit::visit_item(it, (), visit);
}


fn check_crate(tcx: ty::ctxt, crate: @ast::crate,
lint_opts: [(lint, level)], time_pass: bool) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/compile-fail/warn-path-statement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// compile-flags: -W err-path-statement
fn main() {

let x = 10;
x; //! ERROR path statement with no effect
}

0 comments on commit 1f92538

Please sign in to comment.