Skip to content

Commit a791bf8

Browse files
bors[bot]phansch
andcommitted
Merge #3372
3372: UI test cleanup: Extract explicit_counter_loop tests r=matthiaskrgr a=phansch cc #2038 Co-authored-by: Philipp Hansch <[email protected]>
2 parents 14d2700 + 18b1220 commit a791bf8

File tree

4 files changed

+191
-177
lines changed

4 files changed

+191
-177
lines changed

tests/ui/explicit_counter_loop.rs

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
#![warn(clippy::explicit_counter_loop)]
11+
12+
fn main() {
13+
let mut vec = vec![1, 2, 3, 4];
14+
let mut _index = 0;
15+
for _v in &vec {
16+
_index += 1
17+
}
18+
19+
let mut _index = 1;
20+
_index = 0;
21+
for _v in &vec {
22+
_index += 1
23+
}
24+
}
25+
26+
mod issue_1219 {
27+
pub fn test() {
28+
// should not trigger the lint because variable is used after the loop #473
29+
let vec = vec![1,2,3];
30+
let mut index = 0;
31+
for _v in &vec { index += 1 }
32+
println!("index: {}", index);
33+
34+
// should not trigger the lint because the count is conditional #1219
35+
let text = "banana";
36+
let mut count = 0;
37+
for ch in text.chars() {
38+
if ch == 'a' {
39+
continue;
40+
}
41+
count += 1;
42+
println!("{}", count);
43+
}
44+
45+
// should not trigger the lint because the count is conditional
46+
let text = "banana";
47+
let mut count = 0;
48+
for ch in text.chars() {
49+
if ch == 'a' {
50+
count += 1;
51+
}
52+
println!("{}", count);
53+
}
54+
55+
// should trigger the lint because the count is not conditional
56+
let text = "banana";
57+
let mut count = 0;
58+
for ch in text.chars() {
59+
count += 1;
60+
if ch == 'a' {
61+
continue;
62+
}
63+
println!("{}", count);
64+
}
65+
66+
// should trigger the lint because the count is not conditional
67+
let text = "banana";
68+
let mut count = 0;
69+
for ch in text.chars() {
70+
count += 1;
71+
for i in 0..2 {
72+
let _ = 123;
73+
}
74+
println!("{}", count);
75+
}
76+
77+
// should not trigger the lint because the count is incremented multiple times
78+
let text = "banana";
79+
let mut count = 0;
80+
for ch in text.chars() {
81+
count += 1;
82+
for i in 0..2 {
83+
count += 1;
84+
}
85+
println!("{}", count);
86+
}
87+
}
88+
}
89+
90+
mod issue_3308 {
91+
pub fn test() {
92+
// should not trigger the lint because the count is incremented multiple times
93+
let mut skips = 0;
94+
let erasures = vec![];
95+
for i in 0..10 {
96+
while erasures.contains(&(i + skips)) {
97+
skips += 1;
98+
}
99+
println!("{}", skips);
100+
}
101+
102+
// should not trigger the lint because the count is incremented multiple times
103+
let mut skips = 0;
104+
for i in 0..10 {
105+
let mut j = 0;
106+
while j < 5 {
107+
skips += 1;
108+
j += 1;
109+
}
110+
println!("{}", skips);
111+
}
112+
113+
// should not trigger the lint because the count is incremented multiple times
114+
let mut skips = 0;
115+
for i in 0..10 {
116+
for j in 0..5 {
117+
skips += 1;
118+
}
119+
println!("{}", skips);
120+
}
121+
}
122+
}

tests/ui/explicit_counter_loop.stderr

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: the variable `_index` is used as a loop counter. Consider using `for (_index, item) in &vec.enumerate()` or similar iterators
2+
--> $DIR/explicit_counter_loop.rs:15:15
3+
|
4+
15 | for _v in &vec {
5+
| ^^^^
6+
|
7+
= note: `-D clippy::explicit-counter-loop` implied by `-D warnings`
8+
9+
error: the variable `_index` is used as a loop counter. Consider using `for (_index, item) in &vec.enumerate()` or similar iterators
10+
--> $DIR/explicit_counter_loop.rs:21:15
11+
|
12+
21 | for _v in &vec {
13+
| ^^^^
14+
15+
error: the variable `count` is used as a loop counter. Consider using `for (count, item) in text.chars().enumerate()` or similar iterators
16+
--> $DIR/explicit_counter_loop.rs:58:19
17+
|
18+
58 | for ch in text.chars() {
19+
| ^^^^^^^^^^^^
20+
21+
error: the variable `count` is used as a loop counter. Consider using `for (count, item) in text.chars().enumerate()` or similar iterators
22+
--> $DIR/explicit_counter_loop.rs:69:19
23+
|
24+
69 | for ch in text.chars() {
25+
| ^^^^^^^^^^^^
26+
27+
error: aborting due to 4 previous errors
28+

tests/ui/for_loop.rs

+1-111
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl Unrelated {
8484
}
8585

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

277277
// Loop with explicit counter variable
278-
let mut _index = 0;
279-
for _v in &vec {
280-
_index += 1
281-
}
282-
283-
let mut _index = 1;
284-
_index = 0;
285-
for _v in &vec {
286-
_index += 1
287-
}
288278

289279
// Potential false positives
290280
let mut _index = 0;
@@ -594,103 +584,3 @@ mod issue_2496 {
594584
unimplemented!()
595585
}
596586
}
597-
598-
mod issue_1219 {
599-
#[warn(clippy::explicit_counter_loop)]
600-
pub fn test() {
601-
// should not trigger the lint because variable is used after the loop #473
602-
let vec = vec![1,2,3];
603-
let mut index = 0;
604-
for _v in &vec { index += 1 }
605-
println!("index: {}", index);
606-
607-
// should not trigger the lint because the count is conditional #1219
608-
let text = "banana";
609-
let mut count = 0;
610-
for ch in text.chars() {
611-
if ch == 'a' {
612-
continue;
613-
}
614-
count += 1;
615-
println!("{}", count);
616-
}
617-
618-
// should not trigger the lint because the count is conditional
619-
let text = "banana";
620-
let mut count = 0;
621-
for ch in text.chars() {
622-
if ch == 'a' {
623-
count += 1;
624-
}
625-
println!("{}", count);
626-
}
627-
628-
// should trigger the lint because the count is not conditional
629-
let text = "banana";
630-
let mut count = 0;
631-
for ch in text.chars() {
632-
count += 1;
633-
if ch == 'a' {
634-
continue;
635-
}
636-
println!("{}", count);
637-
}
638-
639-
// should trigger the lint because the count is not conditional
640-
let text = "banana";
641-
let mut count = 0;
642-
for ch in text.chars() {
643-
count += 1;
644-
for i in 0..2 {
645-
let _ = 123;
646-
}
647-
println!("{}", count);
648-
}
649-
650-
// should not trigger the lint because the count is incremented multiple times
651-
let text = "banana";
652-
let mut count = 0;
653-
for ch in text.chars() {
654-
count += 1;
655-
for i in 0..2 {
656-
count += 1;
657-
}
658-
println!("{}", count);
659-
}
660-
}
661-
}
662-
663-
mod issue_3308 {
664-
#[warn(clippy::explicit_counter_loop)]
665-
pub fn test() {
666-
// should not trigger the lint because the count is incremented multiple times
667-
let mut skips = 0;
668-
let erasures = vec![];
669-
for i in 0..10 {
670-
while erasures.contains(&(i + skips)) {
671-
skips += 1;
672-
}
673-
println!("{}", skips);
674-
}
675-
676-
// should not trigger the lint because the count is incremented multiple times
677-
let mut skips = 0;
678-
for i in 0..10 {
679-
let mut j = 0;
680-
while j < 5 {
681-
skips += 1;
682-
j += 1;
683-
}
684-
println!("{}", skips);
685-
}
686-
687-
// should not trigger the lint because the count is incremented multiple times
688-
let mut skips = 0;
689-
for i in 0..10 {
690-
for j in 0..5 {
691-
skips += 1;
692-
}
693-
println!("{}", skips);
694-
}
695-
}
696-
}

0 commit comments

Comments
 (0)