From 3c3a4549a8df25e9fcdd2e41a50311124cc0906c Mon Sep 17 00:00:00 2001 From: mcarton Date: Mon, 7 Mar 2016 16:55:12 +0100 Subject: [PATCH] Fix tests with inclusive ranges --- src/loops.rs | 5 +++-- tests/compile-fail/for_loop.rs | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/loops.rs b/src/loops.rs index 723236c7ed73..e41402ef85a7 100644 --- a/src/loops.rs +++ b/src/loops.rs @@ -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, diff --git a/tests/compile-fail/for_loop.rs b/tests/compile-fail/for_loop.rs index 8d483ccde7ab..0853ae83cd7d 100644 --- a/tests/compile-fail/for_loop.rs +++ b/tests/compile-fail/for_loop.rs @@ -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); }