summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/explicit_counter_loop.rs
blob: c25e79a36171f3d194d9a2fedc82c3c382f7d2fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#![warn(clippy::explicit_counter_loop)]
#![allow(clippy::uninlined_format_args, clippy::useless_vec)]
//@no-rustfix
fn main() {
    let mut vec = vec![1, 2, 3, 4];
    let mut _index = 0;
    for _v in &vec {
        //~^ ERROR: the variable `_index` is used as a loop counter
        //~| NOTE: `-D clippy::explicit-counter-loop` implied by `-D warnings`
        _index += 1
    }

    let mut _index = 1;
    _index = 0;
    for _v in &vec {
        //~^ ERROR: the variable `_index` is used as a loop counter
        _index += 1
    }

    let mut _index = 0;
    for _v in &mut vec {
        //~^ ERROR: the variable `_index` is used as a loop counter
        _index += 1;
    }

    let mut _index = 0;
    for _v in vec {
        //~^ ERROR: the variable `_index` is used as a loop counter
        _index += 1;
    }

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

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() {
            println!("{}", count);
            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() {
            println!("{}", count);
            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() {
            //~^ ERROR: the variable `count` is used as a loop counter
            println!("{}", count);
            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() {
            //~^ ERROR: the variable `count` is used as a loop counter
            println!("{}", count);
            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() {
            println!("{}", count);
            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 {
            println!("{}", skips);
            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 {
            println!("{}", skips);
            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 {
            println!("{}", skips);
            for j in 0..5 {
                skips += 1;
            }
        }
    }
}

mod issue_1670 {
    pub fn test() {
        let mut count = 0;
        for _i in 3..10 {
            //~^ ERROR: the variable `count` is used as a loop counter
            count += 1;
        }
    }
}

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

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

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 {
            //~^ ERROR: the variable `idx_usize` is used as a loop counter
            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 {
            //~^ ERROR: the variable `idx_u32` is used as a loop counter
            //~| NOTE: `idx_u32` is of type `u32`, making it ineligible for `Iterator::enumera
            if idx_u32 == index_u32 {
                break;
            }

            idx_u32 += 1;
        }
    }
}

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