]> git.lizzy.rs Git - rust.git/blob - tests/ui/for_loop_fixable.fixed
Auto merge of #4809 - iankronquist:patch-1, r=flip1995
[rust.git] / tests / ui / for_loop_fixable.fixed
1 // run-rustfix
2
3 #![allow(dead_code, unused)]
4
5 use std::collections::*;
6
7 #[warn(clippy::all)]
8 struct Unrelated(Vec<u8>);
9 impl Unrelated {
10     fn next(&self) -> std::slice::Iter<u8> {
11         self.0.iter()
12     }
13
14     fn iter(&self) -> std::slice::Iter<u8> {
15         self.0.iter()
16     }
17 }
18
19 #[warn(
20     clippy::needless_range_loop,
21     clippy::explicit_iter_loop,
22     clippy::explicit_into_iter_loop,
23     clippy::iter_next_loop,
24     clippy::reverse_range_loop,
25     clippy::for_kv_map
26 )]
27 #[allow(
28     clippy::linkedlist,
29     clippy::shadow_unrelated,
30     clippy::unnecessary_mut_passed,
31     clippy::cognitive_complexity,
32     clippy::similar_names
33 )]
34 #[allow(clippy::many_single_char_names, unused_variables)]
35 fn main() {
36     const MAX_LEN: usize = 42;
37     let mut vec = vec![1, 2, 3, 4];
38
39     for i in (0..10).rev() {
40         println!("{}", i);
41     }
42
43     for i in (0..=10).rev() {
44         println!("{}", i);
45     }
46
47     for i in (0..MAX_LEN).rev() {
48         println!("{}", i);
49     }
50
51     for i in 5..=5 {
52         // not an error, this is the range with only one element “5”
53         println!("{}", i);
54     }
55
56     for i in 0..10 {
57         // not an error, the start index is less than the end index
58         println!("{}", i);
59     }
60
61     for i in -10..0 {
62         // not an error
63         println!("{}", i);
64     }
65
66     for i in (10..0).map(|x| x * 2) {
67         // not an error, it can't be known what arbitrary methods do to a range
68         println!("{}", i);
69     }
70
71     // testing that the empty range lint folds constants
72     for i in (5 + 4..10).rev() {
73         println!("{}", i);
74     }
75
76     for i in ((3 - 1)..(5 + 2)).rev() {
77         println!("{}", i);
78     }
79
80     for i in (2 * 2)..(2 * 3) {
81         // no error, 4..6 is fine
82         println!("{}", i);
83     }
84
85     let x = 42;
86     for i in x..10 {
87         // no error, not constant-foldable
88         println!("{}", i);
89     }
90
91     // See #601
92     for i in 0..10 {
93         // no error, id_col does not exist outside the loop
94         let mut id_col = vec![0f64; 10];
95         id_col[i] = 1f64;
96     }
97
98     for _v in &vec {}
99
100     for _v in &mut vec {}
101
102     let out_vec = vec![1, 2, 3];
103     for _v in out_vec {}
104
105     for _v in &vec {} // these are fine
106     for _v in &mut vec {} // these are fine
107
108     for _v in &[1, 2, 3] {}
109
110     for _v in (&mut [1, 2, 3]).iter() {} // no error
111
112     for _v in &[0; 32] {}
113
114     for _v in [0; 33].iter() {} // no error
115
116     let ll: LinkedList<()> = LinkedList::new();
117     for _v in &ll {}
118
119     let vd: VecDeque<()> = VecDeque::new();
120     for _v in &vd {}
121
122     let bh: BinaryHeap<()> = BinaryHeap::new();
123     for _v in &bh {}
124
125     let hm: HashMap<(), ()> = HashMap::new();
126     for _v in &hm {}
127
128     let bt: BTreeMap<(), ()> = BTreeMap::new();
129     for _v in &bt {}
130
131     let hs: HashSet<()> = HashSet::new();
132     for _v in &hs {}
133
134     let bs: BTreeSet<()> = BTreeSet::new();
135     for _v in &bs {}
136
137     let u = Unrelated(vec![]);
138     for _v in u.next() {} // no error
139     for _v in u.iter() {} // no error
140
141     let mut out = vec![];
142     vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
143     let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
144
145     // Loop with explicit counter variable
146
147     // Potential false positives
148     let mut _index = 0;
149     _index = 1;
150     for _v in &vec {
151         _index += 1
152     }
153
154     let mut _index = 0;
155     _index += 1;
156     for _v in &vec {
157         _index += 1
158     }
159
160     let mut _index = 0;
161     if true {
162         _index = 1
163     }
164     for _v in &vec {
165         _index += 1
166     }
167
168     let mut _index = 0;
169     let mut _index = 1;
170     for _v in &vec {
171         _index += 1
172     }
173
174     let mut _index = 0;
175     for _v in &vec {
176         _index += 1;
177         _index += 1
178     }
179
180     let mut _index = 0;
181     for _v in &vec {
182         _index *= 2;
183         _index += 1
184     }
185
186     let mut _index = 0;
187     for _v in &vec {
188         _index = 1;
189         _index += 1
190     }
191
192     let mut _index = 0;
193
194     for _v in &vec {
195         let mut _index = 0;
196         _index += 1
197     }
198
199     let mut _index = 0;
200     for _v in &vec {
201         _index += 1;
202         _index = 0;
203     }
204
205     let mut _index = 0;
206     for _v in &vec {
207         for _x in 0..1 {
208             _index += 1;
209         }
210         _index += 1
211     }
212
213     let mut _index = 0;
214     for x in &vec {
215         if *x == 1 {
216             _index += 1
217         }
218     }
219
220     let mut _index = 0;
221     if true {
222         _index = 1
223     };
224     for _v in &vec {
225         _index += 1
226     }
227
228     let mut _index = 1;
229     if false {
230         _index = 0
231     };
232     for _v in &vec {
233         _index += 1
234     }
235
236     let mut index = 0;
237     {
238         let mut _x = &mut index;
239     }
240     for _v in &vec {
241         _index += 1
242     }
243
244     let mut index = 0;
245     for _v in &vec {
246         index += 1
247     }
248     println!("index: {}", index);
249
250     fn f<T>(_: &T, _: &T) -> bool {
251         unimplemented!()
252     }
253     fn g<T>(_: &mut [T], _: usize, _: usize) {
254         unimplemented!()
255     }
256     for i in 1..vec.len() {
257         if f(&vec[i - 1], &vec[i]) {
258             g(&mut vec, i - 1, i);
259         }
260     }
261
262     for mid in 1..vec.len() {
263         let (_, _) = vec.split_at(mid);
264     }
265 }
266
267 fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
268     let pivot = v.len() - 1;
269     let mut i = 0;
270     for j in 0..pivot {
271         if v[j] <= v[pivot] {
272             v.swap(i, j);
273             i += 1;
274         }
275     }
276     v.swap(i, pivot);
277     i
278 }
279
280 #[warn(clippy::needless_range_loop)]
281 pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
282     // Same source and destination - don't trigger lint
283     for i in 0..dst.len() {
284         dst[d + i] = dst[s + i];
285     }
286 }
287
288 mod issue_2496 {
289     pub trait Handle {
290         fn new_for_index(index: usize) -> Self;
291         fn index(&self) -> usize;
292     }
293
294     pub fn test<H: Handle>() -> H {
295         for x in 0..5 {
296             let next_handle = H::new_for_index(x);
297             println!("{}", next_handle.index());
298         }
299         unimplemented!()
300     }
301 }
302
303 // explicit_into_iter_loop bad suggestions
304 #[warn(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop)]
305 mod issue_4958 {
306     fn takes_iterator<T>(iterator: &T)
307     where
308         for<'a> &'a T: IntoIterator<Item = &'a String>,
309     {
310         for i in iterator {
311             println!("{}", i);
312         }
313     }
314
315     struct T;
316     impl IntoIterator for &T {
317         type Item = ();
318         type IntoIter = std::vec::IntoIter<Self::Item>;
319         fn into_iter(self) -> Self::IntoIter {
320             vec![].into_iter()
321         }
322     }
323
324     fn more_tests() {
325         let t = T;
326         let r = &t;
327         let rr = &&t;
328
329         // This case is handled by `explicit_iter_loop`. No idea why.
330         for _ in &t {}
331
332         for _ in r {}
333
334         // No suggestion for this.
335         // We'd have to suggest `for _ in *rr {}` which is less clear.
336         for _ in rr.into_iter() {}
337     }
338 }