From 7c21ccc4835326e87d6baaaaa8d1c616a398d924 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 1 Aug 2013 19:36:56 +1000 Subject: [PATCH] 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. --- src/librustc/middle/lint.rs | 27 +++++++++++++++++++ .../compile-fail/lint-deprecated-for-loop.rs | 20 ++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/test/compile-fail/lint-deprecated-for-loop.rs diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index c8d4901752c..f972406ae11 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -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 in \ + ` or `do`") + } + _ => {} + } + visit::visit_expr(e, (cx, vt)); + }, + .. *visit::default_visitor() + }) +} + fn lint_type_limits() -> visit::vt<@mut Context> { fn is_valid(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()); diff --git a/src/test/compile-fail/lint-deprecated-for-loop.rs b/src/test/compile-fail/lint-deprecated-for-loop.rs new file mode 100644 index 00000000000..5570562cf8b --- /dev/null +++ b/src/test/compile-fail/lint-deprecated-for-loop.rs @@ -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 or the MIT license +// , 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 +}