Fix tests with inclusive ranges

This commit is contained in:
mcarton 2016-03-07 16:55:12 +01:00
parent 7b135efa73
commit 3c3a4549a8
2 changed files with 25 additions and 3 deletions

View file

@ -10,6 +10,7 @@ use rustc_front::hir::*;
use rustc_front::intravisit::{Visitor, walk_expr, walk_block, walk_decl};
use std::borrow::Cow;
use std::collections::HashMap;
use syntax::ast;
use utils::{snippet, span_lint, get_parent_expr, match_trait_method, match_type, in_external_macro,
span_help_and_lint, is_integer_literal, get_enclosing_block, span_lint_and_then,
@ -417,7 +418,7 @@ fn is_len_call(expr: &Expr, var: &Name) -> bool {
fn check_for_loop_reverse_range(cx: &LateContext, arg: &Expr, expr: &Expr) {
// if this for loop is iterating over a two-sided range...
if let Some(UnsugaredRange { start: Some(ref start), end: Some(ref end), .. }) = unsugar_range(&arg) {
if let Some(UnsugaredRange { start: Some(ref start), end: Some(ref end), limits }) = unsugar_range(&arg) {
// ...and both sides are compile-time constant integers...
if let Ok(start_idx) = eval_const_expr_partial(&cx.tcx, start, ExprTypeChecked, None) {
if let Ok(end_idx) = eval_const_expr_partial(&cx.tcx, end, ExprTypeChecked, None) {
@ -450,7 +451,7 @@ fn check_for_loop_reverse_range(cx: &LateContext, arg: &Expr, expr: &Expr) {
over this range in reverse",
format!("({}..{}).rev()` ", end_snippet, start_snippet));
});
} else if eq {
} else if eq && limits != ast::RangeLimits::Closed {
// if they are equal, it's also problematic - this loop
// will never run.
span_lint(cx,

View file

@ -1,4 +1,4 @@
#![feature(plugin, step_by)]
#![feature(plugin, step_by, inclusive_range_syntax)]
#![plugin(clippy)]
use std::collections::*;
@ -118,11 +118,21 @@ fn main() {
println!("{}", vec[i]);
}
for i in 0...MAX_LEN {
//~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(MAX_LEN)`
println!("{}", vec[i]);
}
for i in 5..10 {
//~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(10).skip(5)`
println!("{}", vec[i]);
}
for i in 5...10 {
//~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(10).skip(5)`
println!("{}", vec[i]);
}
for i in 5..vec.len() {
//~^ ERROR `i` is used to index `vec`. Consider using `for (i, item) in vec.iter().enumerate().skip(5)`
println!("{} {}", vec[i], i);
@ -140,6 +150,13 @@ fn main() {
println!("{}", i);
}
for i in 10...0 {
//~^ERROR this range is empty so this for loop will never run
//~|HELP consider
//~|SUGGESTION (0..10).rev()
println!("{}", i);
}
for i in MAX_LEN..0 { //~ERROR this range is empty so this for loop will never run
//~|HELP consider
//~|SUGGESTION (0..MAX_LEN).rev()
@ -150,6 +167,10 @@ fn main() {
println!("{}", i);
}
for i in 5...5 { // not an error, this is the range with only one element “5”
println!("{}", i);
}
for i in 0..10 { // not an error, the start index is less than the end index
println!("{}", i);
}