summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.rs
blob: 02f80cc52ac72da6e6d60851e4a8cea7d4e0b69a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//@no-rustfix: overlapping suggestions
#![feature(lint_reasons)]
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
#![allow(
    clippy::if_same_then_else,
    clippy::branches_sharing_code,
    clippy::unnecessary_literal_unwrap
)]

macro_rules! m {
    ($a:expr) => {
        if $a.is_some() {
            // unnecessary
            $a.unwrap();
        }
    };
}

macro_rules! checks_in_param {
    ($a:expr, $b:expr) => {
        if $a {
            $b;
        }
    };
}

macro_rules! checks_unwrap {
    ($a:expr, $b:expr) => {
        if $a.is_some() {
            $b;
        }
    };
}

macro_rules! checks_some {
    ($a:expr, $b:expr) => {
        if $a {
            $b.unwrap();
        }
    };
}

fn main() {
    let x = Some(());
    if x.is_some() {
        // unnecessary
        x.unwrap();
        //~^ ERROR: called `unwrap` on `x` after checking its variant with `is_some`
        // unnecessary
        x.expect("an error message");
        //~^ ERROR: called `expect` on `x` after checking its variant with `is_some`
    } else {
        // will panic
        x.unwrap();
        //~^ ERROR: this call to `unwrap()` will always panic
        // will panic
        x.expect("an error message");
        //~^ ERROR: this call to `expect()` will always panic
    }
    if x.is_none() {
        // will panic
        x.unwrap();
        //~^ ERROR: this call to `unwrap()` will always panic
    } else {
        // unnecessary
        x.unwrap();
        //~^ ERROR: called `unwrap` on `x` after checking its variant with `is_none`
    }
    m!(x);
    // ok
    checks_in_param!(x.is_some(), x.unwrap());
    // ok
    checks_unwrap!(x, x.unwrap());
    // ok
    checks_some!(x.is_some(), x);
    let mut x: Result<(), ()> = Ok(());
    if x.is_ok() {
        // unnecessary
        x.unwrap();
        //~^ ERROR: called `unwrap` on `x` after checking its variant with `is_ok`
        // unnecessary
        x.expect("an error message");
        //~^ ERROR: called `expect` on `x` after checking its variant with `is_ok`
        // will panic
        x.unwrap_err();
        //~^ ERROR: this call to `unwrap_err()` will always panic
    } else {
        // will panic
        x.unwrap();
        //~^ ERROR: this call to `unwrap()` will always panic
        // will panic
        x.expect("an error message");
        //~^ ERROR: this call to `expect()` will always panic
        // unnecessary
        x.unwrap_err();
        //~^ ERROR: called `unwrap_err` on `x` after checking its variant with `is_ok`
    }
    if x.is_err() {
        // will panic
        x.unwrap();
        //~^ ERROR: this call to `unwrap()` will always panic
        // unnecessary
        x.unwrap_err();
        //~^ ERROR: called `unwrap_err` on `x` after checking its variant with `is_err`
    } else {
        // unnecessary
        x.unwrap();
        //~^ ERROR: called `unwrap` on `x` after checking its variant with `is_err`
        // will panic
        x.unwrap_err();
        //~^ ERROR: this call to `unwrap_err()` will always panic
    }
    if x.is_ok() {
        x = Err(());
        // not unnecessary because of mutation of x
        // it will always panic but the lint is not smart enough to see this (it only
        // checks if conditions).
        x.unwrap();
    } else {
        x = Ok(());
        // not unnecessary because of mutation of x
        // it will always panic but the lint is not smart enough to see this (it
        // only checks if conditions).
        x.unwrap_err();
    }

    // ok, it's a common test pattern
    assert!(x.is_ok(), "{:?}", x.unwrap_err());
}

fn issue11371() {
    let option = Some(());

    if option.is_some() {
        option.as_ref().unwrap();
        //~^ ERROR: called `unwrap` on `option` after checking its variant with `is_some`
    } else {
        option.as_ref().unwrap();
        //~^ ERROR: this call to `unwrap()` will always panic
    }

    let result = Ok::<(), ()>(());

    if result.is_ok() {
        result.as_ref().unwrap();
        //~^ ERROR: called `unwrap` on `result` after checking its variant with `is_ok`
    } else {
        result.as_ref().unwrap();
        //~^ ERROR: this call to `unwrap()` will always panic
    }

    let mut option = Some(());
    if option.is_some() {
        option.as_mut().unwrap();
        //~^ ERROR: called `unwrap` on `option` after checking its variant with `is_some`
    } else {
        option.as_mut().unwrap();
        //~^ ERROR: this call to `unwrap()` will always panic
    }

    let mut result = Ok::<(), ()>(());
    if result.is_ok() {
        result.as_mut().unwrap();
        //~^ ERROR: called `unwrap` on `result` after checking its variant with `is_ok`
    } else {
        result.as_mut().unwrap();
        //~^ ERROR: this call to `unwrap()` will always panic
    }

    // This should not lint. Statics are, at the time of writing, not linted on anyway,
    // but if at some point they are supported by this lint, it should correctly see that
    // `X` is being mutated and not suggest `if let Some(..) = X {}`
    static mut X: Option<i32> = Some(123);
    unsafe {
        if X.is_some() {
            X = None;
            X.unwrap();
        }
    }
}

fn check_expect() {
    let x = Some(());
    if x.is_some() {
        #[expect(clippy::unnecessary_unwrap)]
        // unnecessary
        x.unwrap();
        #[expect(clippy::unnecessary_unwrap)]
        // unnecessary
        x.expect("an error message");
    } else {
        #[expect(clippy::panicking_unwrap)]
        // will panic
        x.unwrap();
        #[expect(clippy::panicking_unwrap)]
        // will panic
        x.expect("an error message");
    }
}