Skip to content

Commit f609090

Browse files
committed
new lint: using for i in 0..x { .. vec[i] .. } instead of iterator (fixes #3)
1 parent 4400aae commit f609090

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub mod unicode;
3232
pub mod strings;
3333
pub mod methods;
3434
pub mod returns;
35+
pub mod loops;
3536

3637
#[plugin_registrar]
3738
pub fn plugin_registrar(reg: &mut Registry) {
@@ -59,6 +60,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
5960
reg.register_lint_pass(box returns::ReturnPass as LintPassObject);
6061
reg.register_lint_pass(box methods::MethodsPass as LintPassObject);
6162
reg.register_lint_pass(box types::LetPass as LintPassObject);
63+
reg.register_lint_pass(box loops::LoopsPass as LintPassObject);
6264

6365
reg.register_lint_group("clippy", vec![types::BOX_VEC, types::LINKEDLIST,
6466
misc::SINGLE_MATCH,
@@ -87,5 +89,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
8789
methods::STR_TO_STRING,
8890
methods::STRING_TO_STRING,
8991
types::LET_UNIT_VALUE,
92+
loops::NEEDLESS_RANGE_LOOP,
9093
]);
9194
}

src/loops.rs

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use rustc::lint::*;
2+
use syntax::ast::*;
3+
use syntax::visit::{Visitor, walk_expr};
4+
use std::collections::HashSet;
5+
6+
use utils::{span_lint, get_parent_expr};
7+
8+
declare_lint!{ pub NEEDLESS_RANGE_LOOP, Warn,
9+
"Warn about looping over a range of indices if a normal iterator would do" }
10+
11+
#[derive(Copy, Clone)]
12+
pub struct LoopsPass;
13+
14+
impl LintPass for LoopsPass {
15+
fn get_lints(&self) -> LintArray {
16+
lint_array!(NEEDLESS_RANGE_LOOP)
17+
}
18+
19+
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
20+
if let Some((pat, arg, body)) = recover_for_loop(expr) {
21+
// the var must be a single name
22+
if let PatIdent(_, ref ident, _) = pat.node {
23+
// the iteratee must be a range literal
24+
if let ExprRange(_, _) = arg.node {
25+
let mut visitor = VarVisitor { cx: cx, var: ident.node.name,
26+
indexed: HashSet::new(), nonindex: false };
27+
walk_expr(&mut visitor, body);
28+
// linting condition: we only indexed one variable
29+
if visitor.indexed.len() == 1 {
30+
let indexed = visitor.indexed.into_iter().next().unwrap();
31+
if visitor.nonindex {
32+
span_lint(cx, NEEDLESS_RANGE_LOOP, expr.span, &format!(
33+
"the loop variable `{}` is used to index `{}`. Consider using \
34+
`for ({}, item) in {}.iter().enumerate()` or similar iterators.",
35+
ident.node.name.as_str(), indexed.as_str(),
36+
ident.node.name.as_str(), indexed.as_str()));
37+
} else {
38+
span_lint(cx, NEEDLESS_RANGE_LOOP, expr.span, &format!(
39+
"the loop variable `{}` is only used to index `{}`. \
40+
Consider using `for item in &{}` or similar iterators.",
41+
ident.node.name.as_str(), indexed.as_str(), indexed.as_str()));
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
50+
/// Recover the essential nodes of a desugared for loop:
51+
/// `for pat in arg { body }` becomes `(pat, arg, body)`.
52+
fn recover_for_loop<'a>(expr: &'a Expr) -> Option<(&'a Pat, &'a Expr, &'a Expr)> {
53+
if_let_chain! {
54+
[
55+
let ExprMatch(ref iterexpr, ref arms, _) = expr.node,
56+
let ExprCall(_, ref iterargs) = iterexpr.node,
57+
iterargs.len() == 1,
58+
arms.len() == 1 && arms[0].guard.is_none(),
59+
let ExprLoop(ref block, _) = arms[0].body.node,
60+
block.stmts.is_empty(),
61+
let Some(ref loopexpr) = block.expr,
62+
let ExprMatch(_, ref innerarms, MatchSource::ForLoopDesugar) = loopexpr.node,
63+
innerarms.len() == 2 && innerarms[0].pats.len() == 1,
64+
let PatEnum(_, Some(ref somepats)) = innerarms[0].pats[0].node,
65+
somepats.len() == 1
66+
], {
67+
return Some((&*somepats[0],
68+
&*iterargs[0],
69+
&*innerarms[0].body));
70+
}
71+
}
72+
None
73+
}
74+
75+
struct VarVisitor<'v, 't: 'v> {
76+
cx: &'v Context<'v, 't>, // context reference
77+
var: Name, // var name to look for as index
78+
indexed: HashSet<Name>, // indexed variables
79+
nonindex: bool, // has the var been used otherwise?
80+
}
81+
82+
impl<'v, 't> Visitor<'v> for VarVisitor<'v, 't> {
83+
fn visit_expr(&mut self, expr: &'v Expr) {
84+
if let ExprPath(None, ref path) = expr.node {
85+
if path.segments.len() == 1 && path.segments[0].identifier.name == self.var {
86+
// we are referencing our variable! now check if it's as an index
87+
if_let_chain! {
88+
[
89+
let Some(parexpr) = get_parent_expr(self.cx, expr),
90+
let ExprIndex(ref seqexpr, _) = parexpr.node,
91+
let ExprPath(None, ref seqvar) = seqexpr.node,
92+
seqvar.segments.len() == 1
93+
], {
94+
self.indexed.insert(seqvar.segments[0].identifier.name);
95+
return; // no need to walk further
96+
}
97+
}
98+
// we are not indexing anything, record that
99+
self.nonindex = true;
100+
return;
101+
}
102+
}
103+
walk_expr(self, expr);
104+
}
105+
}

tests/compile-fail/for_loop.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
4+
#[deny(needless_range_loop)]
5+
fn main() {
6+
let vec = vec![1, 2, 3, 4];
7+
let vec2 = vec![1, 2, 3, 4];
8+
for i in 0..vec.len() { //~ERROR the loop variable `i` is only used to index `vec`.
9+
println!("{}", vec[i]);
10+
}
11+
for i in 0..vec.len() { //~ERROR the loop variable `i` is used to index `vec`.
12+
println!("{} {}", vec[i], i);
13+
}
14+
for i in 0..vec.len() { // not an error, indexing more than one variable
15+
println!("{} {}", vec[i], vec2[i]);
16+
}
17+
}

0 commit comments

Comments
 (0)