rustc: add a lint for for, suggesting foreach or do.

This is just to aid the transistion to the new `for` loop, by
pointing at each location where the old one occurs.
This commit is contained in:
Huon Wilson 2013-08-01 19:36:56 +10:00 committed by Daniel Micay
parent 78cde5b9fb
commit 7c21ccc483
2 changed files with 47 additions and 0 deletions

View File

@ -74,6 +74,7 @@ pub enum lint {
unused_imports,
unnecessary_qualification,
while_true,
deprecated_for_loop,
path_statement,
unrecognized_lint,
non_camel_case_types,
@ -165,6 +166,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
default: warn
}),
("deprecated_for_loop",
LintSpec {
lint: deprecated_for_loop,
desc: "recommend using `foreach` or `do` instead of `for`",
default: allow
}),
("path_statement",
LintSpec {
lint: path_statement,
@ -561,6 +569,24 @@ fn lint_while_true() -> visit::vt<@mut Context> {
})
}
fn lint_deprecated_for_loop() -> visit::vt<@mut Context> {
visit::mk_vt(@visit::Visitor {
visit_expr: |e, (cx, vt): (@mut Context, visit::vt<@mut Context>)| {
match e.node {
ast::expr_call(_, _, ast::ForSugar) |
ast::expr_method_call(_, _, _, _, _, ast::ForSugar) => {
cx.span_lint(deprecated_for_loop, e.span,
"`for` is deprecated; use `foreach <pat> in \
<iterator>` or `do`")
}
_ => {}
}
visit::visit_expr(e, (cx, vt));
},
.. *visit::default_visitor()
})
}
fn lint_type_limits() -> visit::vt<@mut Context> {
fn is_valid<T:cmp::Ord>(binop: ast::binop, v: T,
min: T, max: T) -> bool {
@ -1096,6 +1122,7 @@ pub fn check_crate(tcx: ty::ctxt, crate: @ast::Crate) {
// Register each of the lint passes with the context
cx.add_lint(lint_while_true());
cx.add_lint(lint_deprecated_for_loop());
cx.add_lint(lint_path_statement());
cx.add_lint(lint_heap());
cx.add_lint(lint_type_limits());

View File

@ -0,0 +1,20 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[forbid(deprecated_for_loop)];
fn f(_: &fn() -> bool) -> bool {
true
}
fn main() {
for f {} //~ ERROR `for` is deprecated
}