Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI test cleanup: Extract explicit_counter_loop tests #3372

Merged
merged 1 commit into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions tests/ui/explicit_counter_loop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![warn(clippy::explicit_counter_loop)]

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
}
}

mod issue_1219 {
pub fn test() {
// should not trigger the lint because variable is used after the loop #473
let vec = vec![1,2,3];
let mut index = 0;
for _v in &vec { index += 1 }
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() {
if ch == 'a' {
continue;
}
count += 1;
println!("{}", count);
}

// should not trigger the lint because the count is conditional
let text = "banana";
let mut count = 0;
for ch in text.chars() {
if ch == 'a' {
count += 1;
}
println!("{}", count);
}

// should trigger the lint because the count is not conditional
let text = "banana";
let mut count = 0;
for ch in text.chars() {
count += 1;
if ch == 'a' {
continue;
}
println!("{}", count);
}

// should trigger the lint because the count is not conditional
let text = "banana";
let mut count = 0;
for ch in text.chars() {
count += 1;
for i in 0..2 {
let _ = 123;
}
println!("{}", count);
}

// should not trigger the lint because the count is incremented multiple times
let text = "banana";
let mut count = 0;
for ch in text.chars() {
count += 1;
for i in 0..2 {
count += 1;
}
println!("{}", count);
}
}
}

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 {
while erasures.contains(&(i + skips)) {
skips += 1;
}
println!("{}", skips);
}

// should not trigger the lint because the count is incremented multiple times
let mut skips = 0;
for i in 0..10 {
let mut j = 0;
while j < 5 {
skips += 1;
j += 1;
}
println!("{}", skips);
}

// should not trigger the lint because the count is incremented multiple times
let mut skips = 0;
for i in 0..10 {
for j in 0..5 {
skips += 1;
}
println!("{}", skips);
}
}
}
28 changes: 28 additions & 0 deletions tests/ui/explicit_counter_loop.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error: the variable `_index` is used as a loop counter. Consider using `for (_index, item) in &vec.enumerate()` or similar iterators
--> $DIR/explicit_counter_loop.rs:15:15
|
15 | for _v in &vec {
| ^^^^
|
= note: `-D clippy::explicit-counter-loop` implied by `-D warnings`

error: the variable `_index` is used as a loop counter. Consider using `for (_index, item) in &vec.enumerate()` or similar iterators
--> $DIR/explicit_counter_loop.rs:21:15
|
21 | for _v in &vec {
| ^^^^

error: the variable `count` is used as a loop counter. Consider using `for (count, item) in text.chars().enumerate()` or similar iterators
--> $DIR/explicit_counter_loop.rs:58:19
|
58 | for ch in text.chars() {
| ^^^^^^^^^^^^

error: the variable `count` is used as a loop counter. Consider using `for (count, item) in text.chars().enumerate()` or similar iterators
--> $DIR/explicit_counter_loop.rs:69:19
|
69 | for ch in text.chars() {
| ^^^^^^^^^^^^

error: aborting due to 4 previous errors

112 changes: 1 addition & 111 deletions tests/ui/for_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Unrelated {
}

#[warn(clippy::needless_range_loop, clippy::explicit_iter_loop, clippy::explicit_into_iter_loop, clippy::iter_next_loop, clippy::reverse_range_loop,
clippy::explicit_counter_loop, clippy::for_kv_map)]
clippy::for_kv_map)]
#[warn(clippy::unused_collect)]
#[allow(clippy::linkedlist, clippy::shadow_unrelated, clippy::unnecessary_mut_passed, clippy::cyclomatic_complexity, clippy::similar_names)]
#[allow(clippy::many_single_char_names, unused_variables)]
Expand Down Expand Up @@ -275,16 +275,6 @@ fn main() {
let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine

// Loop with explicit counter variable
let mut _index = 0;
for _v in &vec {
_index += 1
}

let mut _index = 1;
_index = 0;
for _v in &vec {
_index += 1
}

// Potential false positives
let mut _index = 0;
Expand Down Expand Up @@ -594,103 +584,3 @@ mod issue_2496 {
unimplemented!()
}
}

mod issue_1219 {
#[warn(clippy::explicit_counter_loop)]
pub fn test() {
// should not trigger the lint because variable is used after the loop #473
let vec = vec![1,2,3];
let mut index = 0;
for _v in &vec { index += 1 }
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() {
if ch == 'a' {
continue;
}
count += 1;
println!("{}", count);
}

// should not trigger the lint because the count is conditional
let text = "banana";
let mut count = 0;
for ch in text.chars() {
if ch == 'a' {
count += 1;
}
println!("{}", count);
}

// should trigger the lint because the count is not conditional
let text = "banana";
let mut count = 0;
for ch in text.chars() {
count += 1;
if ch == 'a' {
continue;
}
println!("{}", count);
}

// should trigger the lint because the count is not conditional
let text = "banana";
let mut count = 0;
for ch in text.chars() {
count += 1;
for i in 0..2 {
let _ = 123;
}
println!("{}", count);
}

// should not trigger the lint because the count is incremented multiple times
let text = "banana";
let mut count = 0;
for ch in text.chars() {
count += 1;
for i in 0..2 {
count += 1;
}
println!("{}", count);
}
}
}

mod issue_3308 {
#[warn(clippy::explicit_counter_loop)]
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 {
while erasures.contains(&(i + skips)) {
skips += 1;
}
println!("{}", skips);
}

// should not trigger the lint because the count is incremented multiple times
let mut skips = 0;
for i in 0..10 {
let mut j = 0;
while j < 5 {
skips += 1;
j += 1;
}
println!("{}", skips);
}

// should not trigger the lint because the count is incremented multiple times
let mut skips = 0;
for i in 0..10 {
for j in 0..5 {
skips += 1;
}
println!("{}", skips);
}
}
}
Loading