about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals.rs
blob: 7d0bcc547a42bf6e537348eb2ba527bbe33d0b5b (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
#![allow(
    clippy::if_same_then_else,
    clippy::branches_sharing_code,
    clippy::unnecessary_literal_unwrap
)]

fn test_complex_conditions() {
    let x: Result<(), ()> = Ok(());
    let y: Result<(), ()> = Ok(());
    if x.is_ok() && y.is_err() {
        // unnecessary
        x.unwrap();
        //~^ unnecessary_unwrap

        // will panic
        x.unwrap_err();
        //~^ panicking_unwrap

        // will panic
        y.unwrap();
        //~^ panicking_unwrap

        // unnecessary
        y.unwrap_err();
        //~^ unnecessary_unwrap
    } else {
        // not statically determinable whether any of the following will always succeed or always fail:
        x.unwrap();
        x.unwrap_err();
        y.unwrap();
        y.unwrap_err();
    }

    if x.is_ok() || y.is_ok() {
        // not statically determinable whether any of the following will always succeed or always fail:
        x.unwrap();
        y.unwrap();
    } else {
        // will panic
        x.unwrap();
        //~^ panicking_unwrap

        // unnecessary
        x.unwrap_err();
        //~^ unnecessary_unwrap

        // will panic
        y.unwrap();
        //~^ panicking_unwrap

        // unnecessary
        y.unwrap_err();
        //~^ unnecessary_unwrap
    }
    let z: Result<(), ()> = Ok(());
    if x.is_ok() && !(y.is_ok() || z.is_err()) {
        // unnecessary
        x.unwrap();
        //~^ unnecessary_unwrap

        // will panic
        x.unwrap_err();
        //~^ panicking_unwrap

        // will panic
        y.unwrap();
        //~^ panicking_unwrap

        // unnecessary
        y.unwrap_err();
        //~^ unnecessary_unwrap

        // unnecessary
        z.unwrap();
        //~^ unnecessary_unwrap

        // will panic
        z.unwrap_err();
        //~^ panicking_unwrap
    }
    if x.is_ok() || !(y.is_ok() && z.is_err()) {
        // not statically determinable whether any of the following will always succeed or always fail:
        x.unwrap();
        y.unwrap();
        z.unwrap();
    } else {
        // will panic
        x.unwrap();
        //~^ panicking_unwrap

        // unnecessary
        x.unwrap_err();
        //~^ unnecessary_unwrap

        // unnecessary
        y.unwrap();
        //~^ unnecessary_unwrap

        // will panic
        y.unwrap_err();
        //~^ panicking_unwrap

        // will panic
        z.unwrap();
        //~^ panicking_unwrap

        // unnecessary
        z.unwrap_err();
        //~^ unnecessary_unwrap
    }
}

fn main() {}