2016-03-07 09:55:12 -06:00
|
|
|
#![feature(plugin, step_by, inclusive_range_syntax)]
|
2015-08-12 14:56:27 -05:00
|
|
|
#![plugin(clippy)]
|
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
use std::collections::*;
|
2016-07-01 13:55:45 -05:00
|
|
|
use std::rc::Rc;
|
2015-08-25 11:26:20 -05:00
|
|
|
|
2016-03-07 16:24:11 -06:00
|
|
|
static STATIC: [usize; 4] = [ 0, 1, 8, 16 ];
|
|
|
|
const CONST: [usize; 4] = [ 0, 1, 8, 16 ];
|
|
|
|
|
2016-01-29 01:34:09 -06:00
|
|
|
#[deny(clippy)]
|
2016-01-29 17:15:57 -06:00
|
|
|
fn for_loop_over_option_and_result() {
|
2016-01-29 01:34:09 -06:00
|
|
|
let option = Some(1);
|
2016-01-29 17:15:57 -06:00
|
|
|
let result = option.ok_or("x not found");
|
2016-01-29 01:34:09 -06:00
|
|
|
let v = vec![0,1,2];
|
|
|
|
|
|
|
|
// check FOR_LOOP_OVER_OPTION lint
|
2016-01-29 17:15:57 -06:00
|
|
|
|
2016-01-29 01:34:09 -06:00
|
|
|
for x in option {
|
2016-01-29 17:15:57 -06:00
|
|
|
//~^ ERROR for loop over `option`, which is an `Option`.
|
2016-01-29 01:34:09 -06:00
|
|
|
//~| HELP consider replacing `for x in option` with `if let Some(x) = option`
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
2016-01-29 17:15:57 -06:00
|
|
|
// check FOR_LOOP_OVER_RESULT lint
|
|
|
|
|
|
|
|
for x in result {
|
|
|
|
//~^ ERROR for loop over `result`, which is a `Result`.
|
|
|
|
//~| HELP consider replacing `for x in result` with `if let Ok(x) = result`
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
for x in option.ok_or("x not found") {
|
|
|
|
//~^ ERROR for loop over `option.ok_or("x not found")`, which is a `Result`.
|
|
|
|
//~| HELP consider replacing `for x in option.ok_or("x not found")` with `if let Ok(x) = option.ok_or("x not found")`
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure LOOP_OVER_NEXT lint takes precedence when next() is the last call in the chain
|
|
|
|
|
2016-01-29 01:34:09 -06:00
|
|
|
for x in v.iter().next() {
|
|
|
|
//~^ ERROR you are iterating over `Iterator::next()` which is an Option
|
2016-01-29 17:15:57 -06:00
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we lint when next() is not the last call in the chain
|
|
|
|
|
|
|
|
for x in v.iter().next().and(Some(0)) {
|
|
|
|
//~^ ERROR for loop over `v.iter().next().and(Some(0))`, which is an `Option`
|
|
|
|
//~| HELP consider replacing `for x in v.iter().next().and(Some(0))` with `if let Some(x) = v.iter().next().and(Some(0))`
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
for x in v.iter().next().ok_or("x not found") {
|
|
|
|
//~^ ERROR for loop over `v.iter().next().ok_or("x not found")`, which is a `Result`
|
|
|
|
//~| HELP consider replacing `for x in v.iter().next().ok_or("x not found")` with `if let Ok(x) = v.iter().next().ok_or("x not found")`
|
2016-01-29 01:34:09 -06:00
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for false positives
|
|
|
|
|
|
|
|
// for loop false positive
|
|
|
|
for x in v {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
2016-01-29 17:15:57 -06:00
|
|
|
// while let false positive for Option
|
2016-01-29 01:34:09 -06:00
|
|
|
while let Some(x) = option {
|
|
|
|
println!("{}", x);
|
|
|
|
break;
|
|
|
|
}
|
2016-01-29 17:15:57 -06:00
|
|
|
|
2016-02-07 11:10:03 -06:00
|
|
|
// while let false positive for Result
|
2016-01-29 17:15:57 -06:00
|
|
|
while let Ok(x) = result {
|
|
|
|
println!("{}", x);
|
|
|
|
break;
|
|
|
|
}
|
2016-01-29 01:34:09 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-10 01:33:20 -06:00
|
|
|
#[deny(needless_range_loop, explicit_iter_loop, explicit_into_iter_loop, iter_next_loop, reverse_range_loop, explicit_counter_loop, for_kv_map)]
|
2015-08-30 06:10:59 -05:00
|
|
|
#[deny(unused_collect)]
|
2016-03-01 03:13:54 -06:00
|
|
|
#[allow(linkedlist, shadow_unrelated, unnecessary_mut_passed, cyclomatic_complexity, similar_names)]
|
2016-03-01 06:05:39 -06:00
|
|
|
#[allow(many_single_char_names)]
|
2015-08-12 14:56:27 -05:00
|
|
|
fn main() {
|
2016-02-07 11:10:03 -06:00
|
|
|
const MAX_LEN: usize = 42;
|
|
|
|
|
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() {
|
2016-07-01 11:44:59 -05:00
|
|
|
//~^ ERROR `i` is only used to index `vec`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION for <item> in &vec {
|
2015-08-12 14:56:27 -05:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
2016-03-07 16:24:11 -06:00
|
|
|
|
2016-07-10 07:05:57 -05:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
//~^ WARNING unused variable
|
|
|
|
let i = 42; // make a different `i`
|
|
|
|
println!("{}", vec[i]); // ok, not the `i` of the for-loop
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:44:59 -05:00
|
|
|
for i in 0..vec.len() { let _ = vec[i]; }
|
|
|
|
//~^ ERROR `i` is only used to index `vec`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION for <item> in &vec { let _ = vec[i]; }
|
|
|
|
|
2016-03-07 16:24:11 -06:00
|
|
|
// ICE #746
|
|
|
|
for j in 0..4 {
|
|
|
|
//~^ ERROR `j` is only used to index `STATIC`
|
2016-07-01 11:44:59 -05:00
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION for <item> in STATIC.iter().take(4) {
|
2016-03-07 16:24:11 -06:00
|
|
|
println!("{:?}", STATIC[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for j in 0..4 {
|
|
|
|
//~^ ERROR `j` is only used to index `CONST`
|
2016-07-01 11:44:59 -05:00
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION for <item> in CONST.iter().take(4) {
|
2016-03-07 16:24:11 -06:00
|
|
|
println!("{:?}", CONST[j]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 0..vec.len() {
|
2016-07-01 11:44:59 -05:00
|
|
|
//~^ ERROR `i` is used to index `vec`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION 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() {
|
2016-07-01 11:44:59 -05:00
|
|
|
//~^ ERROR `i` is only used to index `vec2`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION for <item> in vec2.iter().take(vec.len()) {
|
2016-01-14 14:04:34 -06:00
|
|
|
println!("{}", vec2[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 5..vec.len() {
|
2016-07-01 11:44:59 -05:00
|
|
|
//~^ ERROR `i` is only used to index `vec`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION for <item> in vec.iter().skip(5) {
|
2015-09-02 05:41:51 -05:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-02-07 11:10:03 -06:00
|
|
|
for i in 0..MAX_LEN {
|
2016-07-01 11:44:59 -05:00
|
|
|
//~^ ERROR `i` is only used to index `vec`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION for <item> in vec.iter().take(MAX_LEN) {
|
2016-02-07 11:10:03 -06:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-03-07 09:55:12 -06:00
|
|
|
for i in 0...MAX_LEN {
|
2016-07-01 11:44:59 -05:00
|
|
|
//~^ ERROR `i` is only used to index `vec`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
2016-07-01 12:31:14 -05:00
|
|
|
//~| SUGGESTION for <item> in vec.iter().take(MAX_LEN + 1) {
|
2016-03-07 09:55:12 -06:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 5..10 {
|
2016-07-01 11:44:59 -05:00
|
|
|
//~^ ERROR `i` is only used to index `vec`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION for <item> in vec.iter().take(10).skip(5) {
|
2016-01-14 13:58:32 -06:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-03-07 09:55:12 -06:00
|
|
|
for i in 5...10 {
|
2016-07-01 11:44:59 -05:00
|
|
|
//~^ ERROR `i` is only used to index `vec`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
2016-07-01 12:31:14 -05:00
|
|
|
//~| SUGGESTION for <item> in vec.iter().take(10 + 1).skip(5) {
|
2016-03-07 09:55:12 -06:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 5..vec.len() {
|
2016-07-01 11:44:59 -05:00
|
|
|
//~^ ERROR `i` is used to index `vec`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION for (i, <item>) in vec.iter().enumerate().skip(5) {
|
2016-01-14 13:58:32 -06:00
|
|
|
println!("{} {}", vec[i], i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 5..10 {
|
2016-07-01 11:44:59 -05:00
|
|
|
//~^ ERROR `i` is used to index `vec`
|
|
|
|
//~| HELP consider
|
|
|
|
//~| HELP consider
|
|
|
|
//~| SUGGESTION for (i, <item>) in vec.iter().enumerate().take(10).skip(5) {
|
2016-01-14 13:58:32 -06:00
|
|
|
println!("{} {}", vec[i], i);
|
|
|
|
}
|
|
|
|
|
2016-02-07 11:10:03 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-03-07 09:55:12 -06:00
|
|
|
for i in 10...0 {
|
|
|
|
//~^ERROR this range is empty so this for loop will never run
|
|
|
|
//~|HELP consider
|
2016-06-09 16:05:48 -05:00
|
|
|
//~|SUGGESTION (0...10).rev()
|
2016-03-07 09:55:12 -06:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2016-02-07 11:10:03 -06:00
|
|
|
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()
|
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);
|
|
|
|
}
|
|
|
|
|
2016-03-07 09:55:12 -06:00
|
|
|
for i in 5...5 { // not an error, this is the range with only one element “5”
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2015-09-14 19:19:05 -05:00
|
|
|
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).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
|
2016-06-07 11:32:26 -05:00
|
|
|
for i in 10..5+4 {
|
|
|
|
//~^ ERROR this range is empty so this for loop will never run
|
|
|
|
//~| HELP if you are attempting to iterate over this range in reverse
|
|
|
|
//~| SUGGESTION for i in (5+4..10).rev() {
|
2015-09-15 00:20:56 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2016-06-07 11:32:26 -05:00
|
|
|
for i in (5+2)..(3-1) {
|
|
|
|
//~^ ERROR this range is empty so this for loop will never run
|
|
|
|
//~| HELP if you are attempting to iterate over this range in reverse
|
|
|
|
//~| SUGGESTION for i in ((3-1)..(5+2)).rev() {
|
2015-09-15 00:20:56 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-02-13 15:09:17 -06:00
|
|
|
// See #601
|
|
|
|
for i in 0..10 { // no error, id_col does not exist outside the loop
|
|
|
|
let mut id_col = vec![0f64; 10];
|
|
|
|
id_col[i] = 1f64;
|
|
|
|
}
|
|
|
|
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in vec.iter() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&vec`
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &vec {
|
2015-08-13 08:36:31 -05:00
|
|
|
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in vec.iter_mut() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&mut vec`
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &mut vec {
|
2016-09-30 19:01:30 -05:00
|
|
|
|
|
|
|
let out_vec = vec![1,2,3];
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in out_vec.into_iter() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `out_vec` instead of `out_vec.into_iter()`
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in out_vec {
|
2016-09-30 19:01:30 -05:00
|
|
|
|
2015-08-13 08:36:31 -05:00
|
|
|
for _v in &vec { } // these are fine
|
|
|
|
for _v in &mut vec { } // these are fine
|
2015-08-17 00:23:57 -05:00
|
|
|
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in [1, 2, 3].iter() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&[
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &[1, 2, 3] {
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-08-31 01:29:34 -05:00
|
|
|
for _v in (&mut [1, 2, 3]).iter() { } // no error
|
2017-01-28 07:02:49 -06:00
|
|
|
|
|
|
|
for _v in [0; 32].iter() {}
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&[
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &[0; 32] {
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-09-06 06:36:21 -05:00
|
|
|
for _v in [0; 33].iter() {} // no error
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let ll: LinkedList<()> = LinkedList::new();
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in ll.iter() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&ll`
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &ll {
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let vd: VecDeque<()> = VecDeque::new();
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in vd.iter() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&vd`
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &vd {
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let bh: BinaryHeap<()> = BinaryHeap::new();
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in bh.iter() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&bh`
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &bh {
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let hm: HashMap<(), ()> = HashMap::new();
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in hm.iter() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&hm`
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &hm {
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let bt: BTreeMap<(), ()> = BTreeMap::new();
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in bt.iter() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&bt`
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &bt {
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let hs: HashSet<()> = HashSet::new();
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in hs.iter() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&hs`
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &hs {
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let bs: BTreeSet<()> = BTreeSet::new();
|
2017-01-28 07:02:49 -06:00
|
|
|
for _v in bs.iter() { }
|
|
|
|
//~^ ERROR it is more idiomatic to loop over `&bs`
|
|
|
|
//~| HELP to write this more concisely, try looping over
|
2017-01-28 07:12:35 -06:00
|
|
|
//~| SUGGESTION for _v in &bs {
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
|
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);
|
2016-01-29 01:34:09 -06:00
|
|
|
|
2016-01-29 17:15:57 -06:00
|
|
|
for_loop_over_option_and_result();
|
2016-01-19 14:10:00 -06:00
|
|
|
|
|
|
|
let m : HashMap<u64, u64> = HashMap::new();
|
|
|
|
for (_, v) in &m {
|
|
|
|
//~^ you seem to want to iterate on a map's values
|
|
|
|
//~| HELP use the corresponding method
|
2016-07-01 13:55:45 -05:00
|
|
|
//~| HELP use the corresponding method
|
|
|
|
//~| SUGGESTION for v in m.values() {
|
|
|
|
let _v = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
let m : Rc<HashMap<u64, u64>> = Rc::new(HashMap::new());
|
|
|
|
for (_, v) in &*m {
|
|
|
|
//~^ you seem to want to iterate on a map's values
|
|
|
|
//~| HELP use the corresponding method
|
|
|
|
//~| HELP use the corresponding method
|
|
|
|
//~| SUGGESTION for v in (*m).values() {
|
2016-01-19 14:10:00 -06:00
|
|
|
let _v = v;
|
2016-07-01 13:55:45 -05:00
|
|
|
// Here the `*` is not actually necesarry, but the test tests that we don't suggest
|
|
|
|
// `in *m.values()` as we used to
|
2016-01-19 14:10:00 -06:00
|
|
|
}
|
|
|
|
|
2016-02-26 05:45:55 -06:00
|
|
|
let mut m : HashMap<u64, u64> = HashMap::new();
|
|
|
|
for (_, v) in &mut m {
|
2017-01-10 01:33:20 -06:00
|
|
|
//~^ you seem to want to iterate on a map's values
|
|
|
|
//~| HELP use the corresponding method
|
|
|
|
//~| HELP use the corresponding method
|
|
|
|
//~| SUGGESTION for v in m.values_mut()
|
2016-02-26 05:45:55 -06:00
|
|
|
let _v = v;
|
|
|
|
}
|
|
|
|
|
2017-01-10 01:33:20 -06:00
|
|
|
let m: &mut HashMap<u64, u64> = &mut HashMap::new();
|
|
|
|
for (_, v) in &mut *m {
|
|
|
|
//~^ you seem to want to iterate on a map's values
|
|
|
|
//~| HELP use the corresponding method
|
|
|
|
//~| HELP use the corresponding method
|
|
|
|
//~| SUGGESTION for v in (*m).values_mut()
|
|
|
|
let _v = v;
|
|
|
|
}
|
2016-02-26 05:45:55 -06:00
|
|
|
|
2017-01-10 01:33:20 -06:00
|
|
|
let m : HashMap<u64, u64> = HashMap::new();
|
2016-01-19 14:10:00 -06:00
|
|
|
let rm = &m;
|
2016-02-05 12:14:02 -06:00
|
|
|
for (k, _value) in rm {
|
2016-01-19 14:10:00 -06:00
|
|
|
//~^ you seem to want to iterate on a map's keys
|
|
|
|
//~| HELP use the corresponding method
|
2016-07-01 13:55:45 -05:00
|
|
|
//~| HELP use the corresponding method
|
|
|
|
//~| SUGGESTION for k in rm.keys() {
|
2016-01-19 14:10:00 -06:00
|
|
|
let _k = k;
|
|
|
|
}
|
2016-02-05 12:14:02 -06:00
|
|
|
|
|
|
|
test_for_kv_map();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(used_underscore_binding)]
|
|
|
|
fn test_for_kv_map() {
|
|
|
|
let m : HashMap<u64, u64> = HashMap::new();
|
|
|
|
|
|
|
|
// No error, _value is actually used
|
|
|
|
for (k, _value) in &m {
|
|
|
|
let _ = _value;
|
|
|
|
let _k = k;
|
|
|
|
}
|
2015-08-12 14:56:27 -05:00
|
|
|
}
|