about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/manual_unwrap_or.rs
blob: 53cffcab5b56c6fbc5a28cd5fe9ad5395113ac97 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#![allow(dead_code)]
#![allow(
    unused_variables,
    clippy::unnecessary_wraps,
    clippy::unnecessary_literal_unwrap,
    clippy::manual_unwrap_or_default
)]

fn option_unwrap_or() {
    // int case
    match Some(1) {
        //~^ manual_unwrap_or
        Some(i) => i,
        None => 42,
    };

    // int case reversed
    match Some(1) {
        //~^ manual_unwrap_or
        None => 42,
        Some(i) => i,
    };

    // richer none expr
    match Some(1) {
        //~^ manual_unwrap_or
        Some(i) => i,
        None => 1 + 42,
    };

    // multiline case
    #[rustfmt::skip]
    match Some(1) {
    //~^ manual_unwrap_or
        Some(i) => i,
        None => {
            42 + 42
                + 42 + 42 + 42
                + 42 + 42 + 42
        }
    };

    // string case
    match Some("Bob") {
        //~^ manual_unwrap_or
        Some(i) => i,
        None => "Alice",
    };

    // don't lint
    match Some(1) {
        Some(i) => i + 2,
        None => 42,
    };
    match Some(1) {
        Some(i) => i,
        None => return,
    };
    for j in 0..4 {
        match Some(j) {
            Some(i) => i,
            None => continue,
        };
        match Some(j) {
            Some(i) => i,
            None => break,
        };
    }

    // cases where the none arm isn't a constant expression
    // are not linted due to potential ownership issues

    // ownership issue example, don't lint
    struct NonCopyable;
    let mut option: Option<NonCopyable> = None;
    match option {
        Some(x) => x,
        None => {
            option = Some(NonCopyable);
            // some more code ...
            option.unwrap()
        },
    };

    // ownership issue example, don't lint
    let option: Option<&str> = None;
    match option {
        Some(s) => s,
        None => &format!("{} {}!", "hello", "world"),
    };

    if let Some(x) = Some(1) {
        //~^ manual_unwrap_or
        x
    } else {
        42
    };

    //don't lint
    if let Some(x) = Some(1) {
        x + 1
    } else {
        42
    };
    if let Some(x) = Some(1) {
        x
    } else {
        return;
    };
    for j in 0..4 {
        if let Some(x) = Some(j) {
            x
        } else {
            continue;
        };
        if let Some(x) = Some(j) {
            x
        } else {
            break;
        };
    }
}

fn result_unwrap_or() {
    // int case
    match Ok::<i32, &str>(1) {
        //~^ manual_unwrap_or
        Ok(i) => i,
        Err(_) => 42,
    };

    // int case, scrutinee is a binding
    let a = Ok::<i32, &str>(1);
    match a {
        //~^ manual_unwrap_or
        Ok(i) => i,
        Err(_) => 42,
    };

    // int case, suggestion must surround Result expr with parentheses
    match Ok(1) as Result<i32, &str> {
        //~^ manual_unwrap_or
        Ok(i) => i,
        Err(_) => 42,
    };

    // method call case, suggestion must not surround Result expr `s.method()` with parentheses
    struct S;
    impl S {
        fn method(self) -> Option<i32> {
            Some(42)
        }
    }
    let s = S {};
    match s.method() {
        //~^ manual_unwrap_or
        Some(i) => i,
        None => 42,
    };

    // int case reversed
    match Ok::<i32, &str>(1) {
        //~^ manual_unwrap_or
        Err(_) => 42,
        Ok(i) => i,
    };

    // richer none expr
    match Ok::<i32, &str>(1) {
        //~^ manual_unwrap_or
        Ok(i) => i,
        Err(_) => 1 + 42,
    };

    // multiline case
    #[rustfmt::skip]
    match Ok::<i32, &str>(1) {
    //~^ manual_unwrap_or
        Ok(i) => i,
        Err(_) => {
            42 + 42
                + 42 + 42 + 42
                + 42 + 42 + 42
        }
    };

    // string case
    match Ok::<&str, &str>("Bob") {
        //~^ manual_unwrap_or
        Ok(i) => i,
        Err(_) => "Alice",
    };

    // don't lint
    match Ok::<i32, &str>(1) {
        Ok(i) => i + 2,
        Err(_) => 42,
    };
    match Ok::<i32, &str>(1) {
        Ok(i) => i,
        Err(_) => return,
    };
    for j in 0..4 {
        match Ok::<i32, &str>(j) {
            Ok(i) => i,
            Err(_) => continue,
        };
        match Ok::<i32, &str>(j) {
            Ok(i) => i,
            Err(_) => break,
        };
    }

    // don't lint, Err value is used
    match Ok::<&str, &str>("Alice") {
        Ok(s) => s,
        Err(s) => s,
    };
    match Ok::<&str, &str>("Alice") {
        //~^ manual_unwrap_or
        Ok(s) => s,
        Err(s) => "Bob",
    };

    if let Ok(x) = Ok::<i32, i32>(1) {
        //~^ manual_unwrap_or
        x
    } else {
        42
    };

    //don't lint
    if let Ok(x) = Ok::<i32, i32>(1) {
        x + 1
    } else {
        42
    };
    if let Ok(x) = Ok::<i32, i32>(1) {
        x
    } else {
        return;
    };
    for j in 0..4 {
        if let Ok(x) = Ok::<i32, i32>(j) {
            x
        } else {
            continue;
        };
        if let Ok(x) = Ok::<i32, i32>(j) {
            x
        } else {
            break;
        };
    }
}

// don't lint in const fn
const fn const_fn_option_unwrap_or() {
    match Some(1) {
        Some(s) => s,
        None => 0,
    };
}

const fn const_fn_result_unwrap_or() {
    match Ok::<&str, &str>("Alice") {
        Ok(s) => s,
        Err(_) => "Bob",
    };
}

mod issue6965 {
    macro_rules! some_macro {
        () => {
            if 1 > 2 { Some(1) } else { None }
        };
    }

    fn test() {
        let _ = match some_macro!() {
            //~^ manual_unwrap_or
            Some(val) => val,
            None => 0,
        };
    }
}

use std::rc::Rc;
fn format_name(name: Option<&Rc<str>>) -> &str {
    match name {
        None => "<anon>",
        Some(name) => name,
    }
}

fn implicit_deref_ref() {
    let _: &str = match Some(&"bye") {
        None => "hi",
        Some(s) => s,
    };
}

mod issue_13018 {
    use std::collections::HashMap;

    type RefName = i32;
    pub fn get(index: &HashMap<usize, Vec<RefName>>, id: usize) -> &[RefName] {
        if let Some(names) = index.get(&id) { names } else { &[] }
    }

    pub fn get_match(index: &HashMap<usize, Vec<RefName>>, id: usize) -> &[RefName] {
        match index.get(&id) {
            Some(names) => names,
            None => &[],
        }
    }
}

fn implicit_deref(v: Vec<String>) {
    let _ = if let Some(s) = v.first() { s } else { "" };
}

fn allowed_manual_unwrap_or_zero() -> u32 {
    if let Some(x) = Some(42) {
        //~^ manual_unwrap_or
        x
    } else {
        0
    }
}

fn main() {}