2015-09-15 02:12:58 -05:00
|
|
|
#![feature(plugin, step_by)]
|
2015-08-12 14:56:27 -05:00
|
|
|
#![plugin(clippy)]
|
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
use std::collections::*;
|
|
|
|
|
2015-08-17 00:23:57 -05:00
|
|
|
struct Unrelated(Vec<u8>);
|
|
|
|
impl Unrelated {
|
|
|
|
fn next(&self) -> std::slice::Iter<u8> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
2015-08-25 11:26:20 -05:00
|
|
|
|
|
|
|
fn iter(&self) -> std::slice::Iter<u8> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
2015-08-17 00:23:57 -05:00
|
|
|
}
|
|
|
|
|
2015-08-23 12:25:45 -05:00
|
|
|
#[deny(needless_range_loop, explicit_iter_loop, iter_next_loop, reverse_range_loop, explicit_counter_loop)]
|
2015-08-30 06:10:59 -05:00
|
|
|
#[deny(unused_collect)]
|
2015-12-18 18:04:33 -06:00
|
|
|
#[allow(linkedlist,shadow_unrelated,unnecessary_mut_passed, cyclomatic_complexity)]
|
2015-08-12 14:56:27 -05:00
|
|
|
fn main() {
|
2015-08-13 08:36:31 -05:00
|
|
|
let mut vec = vec![1, 2, 3, 4];
|
2015-08-12 14:56:27 -05:00
|
|
|
let vec2 = vec![1, 2, 3, 4];
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
//~^ ERROR `i` is only used to index `vec`. Consider using `for item in &vec`
|
2015-08-12 14:56:27 -05:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
//~^ ERROR `i` is used to index `vec`. Consider using `for (i, item) in vec.iter().enumerate()`
|
2015-08-12 14:56:27 -05:00
|
|
|
println!("{} {}", vec[i], i);
|
|
|
|
}
|
|
|
|
for i in 0..vec.len() { // not an error, indexing more than one variable
|
|
|
|
println!("{} {}", vec[i], vec2[i]);
|
|
|
|
}
|
2015-08-13 08:36:31 -05:00
|
|
|
|
2016-01-14 14:04:34 -06:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
//~^ ERROR `i` is only used to index `vec2`. Consider using `for item in vec2.iter().take(vec.len())`
|
|
|
|
println!("{}", vec2[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 5..vec.len() {
|
|
|
|
//~^ ERROR `i` is only used to index `vec`. Consider using `for item in vec.iter().skip(5)`
|
2015-09-02 05:41:51 -05:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 13:58:32 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 5..10 {
|
|
|
|
//~^ ERROR `i` is used to index `vec`. Consider using `for (i, item) in vec.iter().enumerate().take(10).skip(5)`
|
|
|
|
println!("{} {}", vec[i], i);
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:20:56 -05:00
|
|
|
for i in 10..0 { //~ERROR this range is empty so this for loop will never run
|
2015-09-14 19:19:05 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:20:56 -05:00
|
|
|
for i in 5..5 { //~ERROR this range is empty so this for loop will never run
|
2015-09-14 19:19:05 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..10 { // not an error, the start index is less than the end index
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2015-10-26 01:43:38 -05:00
|
|
|
for i in -10..0 { // not an error
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:20:56 -05:00
|
|
|
for i in (10..0).rev() { // not an error, this is an established idiom for looping backwards on a range
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in (10..0).map(|x| x * 2) { // not an error, it can't be known what arbitrary methods do to a range
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// testing that the empty range lint folds constants
|
|
|
|
for i in 10..5+4 { //~ERROR this range is empty so this for loop will never run
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in (5+2)..(3-1) { //~ERROR this range is empty so this for loop will never run
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in (5+2)..(8-1) { //~ERROR this range is empty so this for loop will never run
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in (2*2)..(2*3) { // no error, 4..6 is fine
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2015-09-15 02:12:58 -05:00
|
|
|
for i in (10..8).step_by(-1) {
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:20:56 -05:00
|
|
|
let x = 42;
|
|
|
|
for i in x..10 { // no error, not constant-foldable
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
for i in (10..0).map(|x| x * 2) {
|
|
|
|
println!("{}", i);
|
|
|
|
}*/
|
|
|
|
|
2015-08-13 08:36:31 -05:00
|
|
|
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`
|
|
|
|
|
|
|
|
for _v in &vec { } // these are fine
|
|
|
|
for _v in &mut vec { } // these are fine
|
2015-08-17 00:23:57 -05:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
for _v in [1, 2, 3].iter() { } //~ERROR it is more idiomatic to loop over `&[
|
2015-08-31 01:29:34 -05:00
|
|
|
for _v in (&mut [1, 2, 3]).iter() { } // no error
|
2015-09-06 06:36:21 -05:00
|
|
|
for _v in [0; 32].iter() {} //~ERROR it is more idiomatic to loop over `&[
|
|
|
|
for _v in [0; 33].iter() {} // no error
|
2015-08-25 11:26:20 -05:00
|
|
|
let ll: LinkedList<()> = LinkedList::new();
|
|
|
|
for _v in ll.iter() { } //~ERROR it is more idiomatic to loop over `&ll`
|
|
|
|
let vd: VecDeque<()> = VecDeque::new();
|
|
|
|
for _v in vd.iter() { } //~ERROR it is more idiomatic to loop over `&vd`
|
|
|
|
let bh: BinaryHeap<()> = BinaryHeap::new();
|
|
|
|
for _v in bh.iter() { } //~ERROR it is more idiomatic to loop over `&bh`
|
|
|
|
let hm: HashMap<(), ()> = HashMap::new();
|
|
|
|
for _v in hm.iter() { } //~ERROR it is more idiomatic to loop over `&hm`
|
|
|
|
let bt: BTreeMap<(), ()> = BTreeMap::new();
|
|
|
|
for _v in bt.iter() { } //~ERROR it is more idiomatic to loop over `&bt`
|
|
|
|
let hs: HashSet<()> = HashSet::new();
|
|
|
|
for _v in hs.iter() { } //~ERROR it is more idiomatic to loop over `&hs`
|
|
|
|
let bs: BTreeSet<()> = BTreeSet::new();
|
|
|
|
for _v in bs.iter() { } //~ERROR it is more idiomatic to loop over `&bs`
|
|
|
|
|
2015-08-17 00:23:57 -05:00
|
|
|
for _v in vec.iter().next() { } //~ERROR you are iterating over `Iterator::next()`
|
|
|
|
|
|
|
|
let u = Unrelated(vec![]);
|
|
|
|
for _v in u.next() { } // no error
|
2015-08-25 11:26:20 -05:00
|
|
|
for _v in u.iter() { } // no error
|
2015-08-30 06:10:59 -05:00
|
|
|
|
|
|
|
let mut out = vec![];
|
|
|
|
vec.iter().map(|x| out.push(x)).collect::<Vec<_>>(); //~ERROR you are collect()ing an iterator
|
|
|
|
let _y = vec.iter().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
// Loop with explicit counter variable
|
|
|
|
let mut _index = 0;
|
|
|
|
for _v in &vec { _index += 1 } //~ERROR the variable `_index` is used as a loop counter
|
|
|
|
|
|
|
|
let mut _index = 1;
|
|
|
|
_index = 0;
|
|
|
|
for _v in &vec { _index += 1 } //~ERROR the variable `_index` is used as a loop counter
|
|
|
|
|
|
|
|
// Potential false positives
|
|
|
|
let mut _index = 0;
|
|
|
|
_index = 1;
|
|
|
|
for _v in &vec { _index += 1 }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
_index += 1;
|
|
|
|
for _v in &vec { _index += 1 }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
if true { _index = 1 }
|
|
|
|
for _v in &vec { _index += 1 }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
let mut _index = 1;
|
|
|
|
for _v in &vec { _index += 1 }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
for _v in &vec { _index += 1; _index += 1 }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
for _v in &vec { _index *= 2; _index += 1 }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
for _v in &vec { _index = 1; _index += 1 }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
|
|
|
|
for _v in &vec { let mut _index = 0; _index += 1 }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
for _v in &vec { _index += 1; _index = 0; }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
for _v in &vec { for _x in 0..1 { _index += 1; }; _index += 1 }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
for x in &vec { if *x == 1 { _index += 1 } }
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
if true { _index = 1 };
|
|
|
|
for _v in &vec { _index += 1 }
|
|
|
|
|
|
|
|
let mut _index = 1;
|
|
|
|
if false { _index = 0 };
|
|
|
|
for _v in &vec { _index += 1 }
|
2015-09-10 07:33:29 -05:00
|
|
|
|
2015-12-18 18:04:33 -06:00
|
|
|
let mut index = 0;
|
|
|
|
{ let mut _x = &mut index; }
|
2015-09-10 07:33:29 -05:00
|
|
|
for _v in &vec { _index += 1 }
|
2015-11-25 17:09:01 -06:00
|
|
|
|
|
|
|
let mut index = 0;
|
|
|
|
for _v in &vec { index += 1 }
|
|
|
|
println!("index: {}", index);
|
2015-08-12 14:56:27 -05:00
|
|
|
}
|