Skip to content

Commit 3c3a454

Browse files
committed
Fix tests with inclusive ranges
1 parent 7b135ef commit 3c3a454

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/loops.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_front::hir::*;
1010
use rustc_front::intravisit::{Visitor, walk_expr, walk_block, walk_decl};
1111
use std::borrow::Cow;
1212
use std::collections::HashMap;
13+
use syntax::ast;
1314

1415
use utils::{snippet, span_lint, get_parent_expr, match_trait_method, match_type, in_external_macro,
1516
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 {
417418

418419
fn check_for_loop_reverse_range(cx: &LateContext, arg: &Expr, expr: &Expr) {
419420
// if this for loop is iterating over a two-sided range...
420-
if let Some(UnsugaredRange { start: Some(ref start), end: Some(ref end), .. }) = unsugar_range(&arg) {
421+
if let Some(UnsugaredRange { start: Some(ref start), end: Some(ref end), limits }) = unsugar_range(&arg) {
421422
// ...and both sides are compile-time constant integers...
422423
if let Ok(start_idx) = eval_const_expr_partial(&cx.tcx, start, ExprTypeChecked, None) {
423424
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) {
450451
over this range in reverse",
451452
format!("({}..{}).rev()` ", end_snippet, start_snippet));
452453
});
453-
} else if eq {
454+
} else if eq && limits != ast::RangeLimits::Closed {
454455
// if they are equal, it's also problematic - this loop
455456
// will never run.
456457
span_lint(cx,

tests/compile-fail/for_loop.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(plugin, step_by)]
1+
#![feature(plugin, step_by, inclusive_range_syntax)]
22
#![plugin(clippy)]
33

44
use std::collections::*;
@@ -118,11 +118,21 @@ fn main() {
118118
println!("{}", vec[i]);
119119
}
120120

121+
for i in 0...MAX_LEN {
122+
//~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(MAX_LEN)`
123+
println!("{}", vec[i]);
124+
}
125+
121126
for i in 5..10 {
122127
//~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(10).skip(5)`
123128
println!("{}", vec[i]);
124129
}
125130

131+
for i in 5...10 {
132+
//~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().take(10).skip(5)`
133+
println!("{}", vec[i]);
134+
}
135+
126136
for i in 5..vec.len() {
127137
//~^ ERROR `i` is used to index `vec`. Consider using `for (i, item) in vec.iter().enumerate().skip(5)`
128138
println!("{} {}", vec[i], i);
@@ -140,6 +150,13 @@ fn main() {
140150
println!("{}", i);
141151
}
142152

153+
for i in 10...0 {
154+
//~^ERROR this range is empty so this for loop will never run
155+
//~|HELP consider
156+
//~|SUGGESTION (0..10).rev()
157+
println!("{}", i);
158+
}
159+
143160
for i in MAX_LEN..0 { //~ERROR this range is empty so this for loop will never run
144161
//~|HELP consider
145162
//~|SUGGESTION (0..MAX_LEN).rev()
@@ -150,6 +167,10 @@ fn main() {
150167
println!("{}", i);
151168
}
152169

170+
for i in 5...5 { // not an error, this is the range with only one element “5”
171+
println!("{}", i);
172+
}
173+
153174
for i in 0..10 { // not an error, the start index is less than the end index
154175
println!("{}", i);
155176
}

0 commit comments

Comments
 (0)