Only handle ranges starting with 0 for needless_range_loop (fixes #279)

This commit is contained in:
Manish Goregaokar 2015-09-02 16:11:51 +05:30
parent eb3b9b35da
commit 73c34e12b3
2 changed files with 30 additions and 20 deletions

View file

@ -37,7 +37,11 @@ impl LintPass for LoopsPass {
if let Some((pat, arg, body)) = recover_for_loop(expr) {
// check for looping over a range and then indexing a sequence with it
// -> the iteratee must be a range literal
if let ExprRange(_, _) = arg.node {
if let ExprRange(Some(ref l), _) = arg.node {
// Range should start with `0`
if let ExprLit(ref lit) = l.node {
if let LitInt(0, _) = lit.node {
// the var must be a single name
if let PatIdent(_, ref ident, _) = pat.node {
let mut visitor = VarVisitor { cx: cx, var: ident.node.name,
@ -61,6 +65,8 @@ impl LintPass for LoopsPass {
}
}
}
}
}
if let ExprMethodCall(ref method, _, ref args) = arg.node {
// just the receiver, no arguments

View file

@ -30,6 +30,10 @@ fn main() {
println!("{} {}", vec[i], vec2[i]);
}
for i in 5..vec.len() { // not an error, not starting with 0
println!("{}", vec[i]);
}
for _v in vec.iter() { } //~ERROR it is more idiomatic to loop over `&vec`
for _v in vec.iter_mut() { } //~ERROR it is more idiomatic to loop over `&mut vec`