Skip to content

Commit 1f92538

Browse files
committed
Add check for path-statements, close #400.
1 parent 33a296f commit 1f92538

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/rustc/middle/lint.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ at all.
2424
enum lint {
2525
ctypes,
2626
unused_imports,
27-
while_true
27+
while_true,
28+
path_statement,
2829
}
2930

3031
enum level {
@@ -56,7 +57,13 @@ fn get_lint_dict() -> lint_dict {
5657
("while_true",
5758
@{lint: while_true,
5859
desc: "suggest using loop { } instead of while(true) { }",
60+
default: warn}),
61+
62+
("path_statement",
63+
@{lint: path_statement,
64+
desc: "path statements with no effect",
5965
default: warn})
66+
6067
];
6168
hash_from_strs(v)
6269
}
@@ -177,6 +184,7 @@ fn check_item(cx: ctxt, i: @ast::item) {
177184
ctypes { check_item_ctypes(cx, level, i); }
178185
unused_imports { check_item_unused_imports(cx, level, i); }
179186
while_true { check_item_while_true(cx, level, i); }
187+
path_statement { check_item_path_statement(cx, level, i); }
180188
}
181189
}
182190
}
@@ -252,6 +260,25 @@ fn check_item_ctypes(cx: ctxt, level: level, it: @ast::item) {
252260
}
253261
}
254262

263+
fn check_item_path_statement(cx: ctxt, level: level, it: @ast::item) {
264+
let visit = visit::mk_simple_visitor(@{
265+
visit_stmt: fn@(s: @ast::stmt) {
266+
alt s.node {
267+
ast::stmt_semi(@{id: _,
268+
node: ast::expr_path(@path),
269+
span: _}, _) {
270+
cx.span_lint(
271+
level, s.span,
272+
"path statement with no effect");
273+
}
274+
_ {}
275+
}
276+
}
277+
with *visit::default_simple_visitor()
278+
});
279+
visit::visit_item(it, (), visit);
280+
}
281+
255282

256283
fn check_crate(tcx: ty::ctxt, crate: @ast::crate,
257284
lint_opts: [(lint, level)], time_pass: bool) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// compile-flags: -W err-path-statement
2+
fn main() {
3+
4+
let x = 10;
5+
x; //! ERROR path statement with no effect
6+
}

0 commit comments

Comments
 (0)