Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub enum lint {
unused_imports,
unnecessary_qualification,
while_true,
deprecated_for_loop,
path_statement,
unrecognized_lint,
non_camel_case_types,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/lint-deprecated-for-loop.rs
Original file line number Diff line number Diff line change
@@ -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
}