2018-10-27 12:16:43 -05:00
|
|
|
#![warn(clippy::explicit_counter_loop)]
|
2023-06-06 15:56:57 -05:00
|
|
|
#![allow(clippy::uninlined_format_args, clippy::useless_vec)]
|
2018-10-27 12:16:43 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut vec = vec![1, 2, 3, 4];
|
|
|
|
let mut _index = 0;
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut _index = 1;
|
|
|
|
_index = 0;
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2019-10-17 23:03:27 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
for _v in &mut vec {
|
|
|
|
_index += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
for _v in vec {
|
|
|
|
_index += 1;
|
|
|
|
}
|
2023-02-27 11:47:48 -06:00
|
|
|
|
|
|
|
let vec = [1, 2, 3, 4];
|
|
|
|
// 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;
|
|
|
|
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;
|
|
|
|
if true {
|
|
|
|
_index = 1
|
|
|
|
};
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut _index = 1;
|
|
|
|
if false {
|
|
|
|
_index = 0
|
|
|
|
};
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2018-10-27 12:16:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mod issue_1219 {
|
|
|
|
pub fn test() {
|
|
|
|
// should not trigger the lint because variable is used after the loop #473
|
2018-12-09 16:26:16 -06:00
|
|
|
let vec = vec![1, 2, 3];
|
2018-10-27 12:16:43 -05:00
|
|
|
let mut index = 0;
|
2018-12-09 16:26:16 -06:00
|
|
|
for _v in &vec {
|
|
|
|
index += 1
|
|
|
|
}
|
2018-10-27 12:16:43 -05:00
|
|
|
println!("index: {}", index);
|
|
|
|
|
|
|
|
// should not trigger the lint because the count is conditional #1219
|
|
|
|
let text = "banana";
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in text.chars() {
|
2020-06-09 21:05:32 -05:00
|
|
|
println!("{}", count);
|
2018-10-27 12:16:43 -05:00
|
|
|
if ch == 'a' {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not trigger the lint because the count is conditional
|
|
|
|
let text = "banana";
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in text.chars() {
|
2020-06-09 21:05:32 -05:00
|
|
|
println!("{}", count);
|
2018-10-27 12:16:43 -05:00
|
|
|
if ch == 'a' {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// should trigger the lint because the count is not conditional
|
|
|
|
let text = "banana";
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in text.chars() {
|
2020-06-09 21:05:32 -05:00
|
|
|
println!("{}", count);
|
2018-10-27 12:16:43 -05:00
|
|
|
count += 1;
|
|
|
|
if ch == 'a' {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// should trigger the lint because the count is not conditional
|
|
|
|
let text = "banana";
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in text.chars() {
|
2020-06-09 21:05:32 -05:00
|
|
|
println!("{}", count);
|
2018-10-27 12:16:43 -05:00
|
|
|
count += 1;
|
|
|
|
for i in 0..2 {
|
|
|
|
let _ = 123;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not trigger the lint because the count is incremented multiple times
|
|
|
|
let text = "banana";
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in text.chars() {
|
2020-06-09 21:05:32 -05:00
|
|
|
println!("{}", count);
|
2018-10-27 12:16:43 -05:00
|
|
|
count += 1;
|
|
|
|
for i in 0..2 {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod issue_3308 {
|
|
|
|
pub fn test() {
|
|
|
|
// should not trigger the lint because the count is incremented multiple times
|
|
|
|
let mut skips = 0;
|
|
|
|
let erasures = vec![];
|
|
|
|
for i in 0..10 {
|
2020-06-09 21:05:32 -05:00
|
|
|
println!("{}", skips);
|
2018-10-27 12:16:43 -05:00
|
|
|
while erasures.contains(&(i + skips)) {
|
|
|
|
skips += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not trigger the lint because the count is incremented multiple times
|
|
|
|
let mut skips = 0;
|
|
|
|
for i in 0..10 {
|
2020-06-09 21:05:32 -05:00
|
|
|
println!("{}", skips);
|
2018-10-27 12:16:43 -05:00
|
|
|
let mut j = 0;
|
|
|
|
while j < 5 {
|
|
|
|
skips += 1;
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not trigger the lint because the count is incremented multiple times
|
|
|
|
let mut skips = 0;
|
|
|
|
for i in 0..10 {
|
2020-06-09 21:05:32 -05:00
|
|
|
println!("{}", skips);
|
2018-10-27 12:16:43 -05:00
|
|
|
for j in 0..5 {
|
|
|
|
skips += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 01:36:48 -05:00
|
|
|
|
|
|
|
mod issue_1670 {
|
|
|
|
pub fn test() {
|
|
|
|
let mut count = 0;
|
|
|
|
for _i in 3..10 {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-11 04:27:18 -06:00
|
|
|
|
|
|
|
mod issue_4732 {
|
|
|
|
pub fn test() {
|
|
|
|
let slice = &[1, 2, 3];
|
|
|
|
let mut index = 0;
|
|
|
|
|
|
|
|
// should not trigger the lint because the count is used after the loop
|
|
|
|
for _v in slice {
|
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
let _closure = || println!("index: {}", index);
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 21:05:32 -05:00
|
|
|
|
|
|
|
mod issue_4677 {
|
|
|
|
pub fn test() {
|
|
|
|
let slice = &[1, 2, 3];
|
|
|
|
|
|
|
|
// should not trigger the lint because the count is used after incremented
|
|
|
|
let mut count = 0;
|
|
|
|
for _i in slice {
|
|
|
|
count += 1;
|
|
|
|
println!("{}", count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 21:09:26 -06:00
|
|
|
|
|
|
|
mod issue_7920 {
|
|
|
|
pub fn test() {
|
|
|
|
let slice = &[1, 2, 3];
|
|
|
|
|
|
|
|
let index_usize: usize = 0;
|
|
|
|
let mut idx_usize: usize = 0;
|
|
|
|
|
|
|
|
// should suggest `enumerate`
|
|
|
|
for _item in slice {
|
|
|
|
if idx_usize == index_usize {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx_usize += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index_u32: u32 = 0;
|
|
|
|
let mut idx_u32: u32 = 0;
|
|
|
|
|
|
|
|
// should suggest `zip`
|
|
|
|
for _item in slice {
|
|
|
|
if idx_u32 == index_u32 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx_u32 += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 09:22:29 -06:00
|
|
|
|
|
|
|
mod issue_10058 {
|
|
|
|
pub fn test() {
|
|
|
|
// should not lint since we are increasing counter potentially more than once in the loop
|
|
|
|
let values = [0, 1, 0, 1, 1, 1, 0, 1, 0, 1];
|
|
|
|
let mut counter = 0;
|
|
|
|
for value in values {
|
|
|
|
counter += 1;
|
|
|
|
|
|
|
|
if value == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test2() {
|
|
|
|
// should not lint since we are increasing counter potentially more than once in the loop
|
|
|
|
let values = [0, 1, 0, 1, 1, 1, 0, 1, 0, 1];
|
|
|
|
let mut counter = 0;
|
|
|
|
for value in values {
|
|
|
|
counter += 1;
|
|
|
|
|
|
|
|
if value != 0 {
|
|
|
|
counter += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|