Skip to content

Commit 7c21ccc

Browse files
huonwthestinger
authored andcommitted
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.
1 parent 78cde5b commit 7c21ccc

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/librustc/middle/lint.rs

+27
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub enum lint {
7474
unused_imports,
7575
unnecessary_qualification,
7676
while_true,
77+
deprecated_for_loop,
7778
path_statement,
7879
unrecognized_lint,
7980
non_camel_case_types,
@@ -165,6 +166,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
165166
default: warn
166167
}),
167168

169+
("deprecated_for_loop",
170+
LintSpec {
171+
lint: deprecated_for_loop,
172+
desc: "recommend using `foreach` or `do` instead of `for`",
173+
default: allow
174+
}),
175+
168176
("path_statement",
169177
LintSpec {
170178
lint: path_statement,
@@ -561,6 +569,24 @@ fn lint_while_true() -> visit::vt<@mut Context> {
561569
})
562570
}
563571

572+
fn lint_deprecated_for_loop() -> visit::vt<@mut Context> {
573+
visit::mk_vt(@visit::Visitor {
574+
visit_expr: |e, (cx, vt): (@mut Context, visit::vt<@mut Context>)| {
575+
match e.node {
576+
ast::expr_call(_, _, ast::ForSugar) |
577+
ast::expr_method_call(_, _, _, _, _, ast::ForSugar) => {
578+
cx.span_lint(deprecated_for_loop, e.span,
579+
"`for` is deprecated; use `foreach <pat> in \
580+
<iterator>` or `do`")
581+
}
582+
_ => {}
583+
}
584+
visit::visit_expr(e, (cx, vt));
585+
},
586+
.. *visit::default_visitor()
587+
})
588+
}
589+
564590
fn lint_type_limits() -> visit::vt<@mut Context> {
565591
fn is_valid<T:cmp::Ord>(binop: ast::binop, v: T,
566592
min: T, max: T) -> bool {
@@ -1096,6 +1122,7 @@ pub fn check_crate(tcx: ty::ctxt, crate: @ast::Crate) {
10961122

10971123
// Register each of the lint passes with the context
10981124
cx.add_lint(lint_while_true());
1125+
cx.add_lint(lint_deprecated_for_loop());
10991126
cx.add_lint(lint_path_statement());
11001127
cx.add_lint(lint_heap());
11011128
cx.add_lint(lint_type_limits());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
#[forbid(deprecated_for_loop)];
13+
14+
fn f(_: &fn() -> bool) -> bool {
15+
true
16+
}
17+
18+
fn main() {
19+
for f {} //~ ERROR `for` is deprecated
20+
}

0 commit comments

Comments
 (0)