summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/manual_while_let_some.fixed
blob: 8b610919536c0fc26a827b8c722d71bb078de434 (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
//@run-rustfix

#![allow(unused)]
#![warn(clippy::manual_while_let_some)]

struct VecInStruct {
    numbers: Vec<i32>,
    unrelated: String,
}

struct Foo {
    a: i32,
    b: i32,
}

fn accept_i32(_: i32) {}
fn accept_optional_i32(_: Option<i32>) {}
fn accept_i32_tuple(_: (i32, i32)) {}

fn main() {
    let mut numbers = vec![1, 2, 3, 4, 5];
    while let Some(number) = numbers.pop() {
        
    }

    let mut val = VecInStruct {
        numbers: vec![1, 2, 3, 4, 5],
        unrelated: String::new(),
    };
    while let Some(number) = val.numbers.pop() {
        
    }

    while let Some(element) = numbers.pop() {
        accept_i32(element);
    }

    while let Some(element) = numbers.pop() {
        accept_i32(element);
    }

    // This should not warn. It "conditionally" pops elements.
    while !numbers.is_empty() {
        if true {
            accept_i32(numbers.pop().unwrap());
        }
    }

    // This should also not warn. It conditionally pops elements.
    while !numbers.is_empty() {
        if false {
            continue;
        }
        accept_i32(numbers.pop().unwrap());
    }

    // This should not warn. It pops elements, but does not unwrap it.
    // Might handle the Option in some other arbitrary way.
    while !numbers.is_empty() {
        accept_optional_i32(numbers.pop());
    }

    let unrelated_vec: Vec<String> = Vec::new();
    // This should not warn. It pops elements from a different vector.
    while !unrelated_vec.is_empty() {
        accept_i32(numbers.pop().unwrap());
    }

    macro_rules! generate_loop {
        () => {
            while !numbers.is_empty() {
                accept_i32(numbers.pop().unwrap());
            }
        };
    }
    // Do not warn if the loop comes from a macro.
    generate_loop!();

    // Try other kinds of patterns
    let mut numbers = vec![(0, 0), (1, 1), (2, 2)];
    while let Some((a, b)) = numbers.pop() {
        
    }

    while let Some(element) = numbers.pop() {
        accept_i32_tuple(element);
    }

    let mut results = vec![Foo { a: 1, b: 2 }, Foo { a: 3, b: 4 }];
    while let Some(Foo { a, b }) = results.pop() {
        
    }
}