summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/iter_out_of_bounds.rs
blob: 3cfe6e82fc1e31b5894cb201dd049b71b7a61ade (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
//@no-rustfix

#![deny(clippy::iter_out_of_bounds)]
#![allow(clippy::useless_vec)]

fn opaque_empty_iter() -> impl Iterator<Item = ()> {
    std::iter::empty()
}

fn main() {
    #[allow(clippy::never_loop)]
    for _ in [1, 2, 3].iter().skip(4) {
        //~^ ERROR: this `.skip()` call skips more items than the iterator will produce
        unreachable!();
    }
    for (i, _) in [1, 2, 3].iter().take(4).enumerate() {
        //~^ ERROR: this `.take()` call takes more items than the iterator will produce
        assert!(i <= 2);
    }

    #[allow(clippy::needless_borrow)]
    for _ in (&&&&&&[1, 2, 3]).iter().take(4) {}
    //~^ ERROR: this `.take()` call takes more items than the iterator will produce

    for _ in [1, 2, 3].iter().skip(4) {}
    //~^ ERROR: this `.skip()` call skips more items than the iterator will produce

    for _ in [1; 3].iter().skip(4) {}
    //~^ ERROR: this `.skip()` call skips more items than the iterator will produce

    // Should not lint
    for _ in opaque_empty_iter().skip(1) {}

    for _ in vec![1, 2, 3].iter().skip(4) {}
    //~^ ERROR: this `.skip()` call skips more items than the iterator will produce

    for _ in vec![1; 3].iter().skip(4) {}
    //~^ ERROR: this `.skip()` call skips more items than the iterator will produce

    let x = [1, 2, 3];
    for _ in x.iter().skip(4) {}
    //~^ ERROR: this `.skip()` call skips more items than the iterator will produce

    let n = 4;
    for _ in x.iter().skip(n) {}
    //~^ ERROR: this `.skip()` call skips more items than the iterator will produce

    let empty = std::iter::empty::<i8>;

    for _ in empty().skip(1) {}
    //~^ ERROR: this `.skip()` call skips more items than the iterator will produce

    for _ in empty().take(1) {}
    //~^ ERROR: this `.take()` call takes more items than the iterator will produce

    for _ in std::iter::once(1).skip(2) {}
    //~^ ERROR: this `.skip()` call skips more items than the iterator will produce

    for _ in std::iter::once(1).take(2) {}
    //~^ ERROR: this `.take()` call takes more items than the iterator will produce

    for x in [].iter().take(1) {
        //~^ ERROR: this `.take()` call takes more items than the iterator will produce
        let _: &i32 = x;
    }

    // ok, not out of bounds
    for _ in [1].iter().take(1) {}
    for _ in [1, 2, 3].iter().take(2) {}
    for _ in [1, 2, 3].iter().skip(2) {}
}