]> git.lizzy.rs Git - rust.git/blob - tests/ui/infinite_iter.rs
Auto merge of #3519 - phansch:brave_newer_ui_tests, r=flip1995
[rust.git] / tests / ui / infinite_iter.rs
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 use std::iter::repeat;
11 #[allow(clippy::trivially_copy_pass_by_ref)]
12 fn square_is_lower_64(x: &u32) -> bool {
13     x * x < 64
14 }
15
16 #[allow(clippy::maybe_infinite_iter)]
17 #[deny(clippy::infinite_iter)]
18 fn infinite_iters() {
19     repeat(0_u8).collect::<Vec<_>>(); // infinite iter
20     (0..8_u32).take_while(square_is_lower_64).cycle().count(); // infinite iter
21     (0..8_u64).chain(0..).max(); // infinite iter
22     (0_usize..)
23         .chain([0usize, 1, 2].iter().cloned())
24         .skip_while(|x| *x != 42)
25         .min(); // infinite iter
26     (0..8_u32)
27         .rev()
28         .cycle()
29         .map(|x| x + 1_u32)
30         .for_each(|x| println!("{}", x)); // infinite iter
31     (0..3_u32).flat_map(|x| x..).sum::<u32>(); // infinite iter
32     (0_usize..).flat_map(|x| 0..x).product::<usize>(); // infinite iter
33     (0_u64..).filter(|x| x % 2 == 0).last(); // infinite iter
34     (0..42_u64).by_ref().last(); // not an infinite, because ranges are double-ended
35     (0..).next(); // iterator is not exhausted
36 }
37
38 #[deny(clippy::maybe_infinite_iter)]
39 fn potential_infinite_iters() {
40     (0..).zip((0..).take_while(square_is_lower_64)).count(); // maybe infinite iter
41     repeat(42).take_while(|x| *x == 42).chain(0..42).max(); // maybe infinite iter
42     (1..)
43         .scan(0, |state, x| {
44             *state += x;
45             Some(*state)
46         })
47         .min(); // maybe infinite iter
48     (0..).find(|x| *x == 24); // maybe infinite iter
49     (0..).position(|x| x == 24); // maybe infinite iter
50     (0..).any(|x| x == 24); // maybe infinite iter
51     (0..).all(|x| x == 24); // maybe infinite iter
52
53     (0..).zip(0..42).take_while(|&(x, _)| x != 42).count(); // not infinite
54     repeat(42).take_while(|x| *x == 42).next(); // iterator is not exhausted
55 }
56
57 fn main() {
58     infinite_iters();
59     potential_infinite_iters();
60 }
61
62 mod finite_collect {
63     use std::collections::HashSet;
64     use std::iter::FromIterator;
65
66     struct C;
67     impl FromIterator<i32> for C {
68         fn from_iter<I: IntoIterator<Item = i32>>(iter: I) -> Self {
69             C
70         }
71     }
72
73     fn check_collect() {
74         let _: HashSet<i32> = (0..).collect(); // Infinite iter
75
76         // Some data structures don't collect infinitely, such as `ArrayVec`
77         let _: C = (0..).collect();
78     }
79 }