1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
10 use std::iter::repeat;
11 #[allow(clippy::trivially_copy_pass_by_ref)]
12 fn square_is_lower_64(x: &u32) -> bool {
16 #[allow(clippy::maybe_infinite_iter)]
17 #[deny(clippy::infinite_iter)]
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
23 .chain([0usize, 1, 2].iter().cloned())
24 .skip_while(|x| *x != 42)
25 .min(); // infinite iter
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
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
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
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
59 potential_infinite_iters();