about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-08-01 19:36:56 +1000
committerDaniel Micay <danielmicay@gmail.com>2013-08-01 15:07:19 -0400
commit7c21ccc4835326e87d6baaaaa8d1c616a398d924 (patch)
treebd35c175213f9a055fc7a259d99e7a0e080b62be
parent78cde5b9fb9db91f954f7fe4afdd230de6754e54 (diff)
downloadrust-7c21ccc4835326e87d6baaaaa8d1c616a398d924.tar.gz
rust-7c21ccc4835326e87d6baaaaa8d1c616a398d924.zip
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.
-rw-r--r--src/librustc/middle/lint.rs27
-rw-r--r--src/test/compile-fail/lint-deprecated-for-loop.rs20
2 files changed, 47 insertions, 0 deletions
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 <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());
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 <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
+}