]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_unwrap_or.fixed
a8736f1e6efe159c7390fc719585a2a2745369ae
[rust.git] / tests / ui / manual_unwrap_or.fixed
1 // run-rustfix
2 #![allow(dead_code)]
3
4 fn unwrap_or() {
5     // int case
6     Some(1).unwrap_or(42);
7
8     // int case reversed
9     Some(1).unwrap_or(42);
10
11     // richer none expr
12     Some(1).unwrap_or(1 + 42);
13
14     // multiline case
15     #[rustfmt::skip]
16     Some(1).unwrap_or({
17         42 + 42
18             + 42 + 42 + 42
19             + 42 + 42 + 42
20     });
21
22     // string case
23     Some("Bob").unwrap_or("Alice");
24
25     // don't lint
26     match Some(1) {
27         Some(i) => i + 2,
28         None => 42,
29     };
30     match Some(1) {
31         Some(i) => i,
32         None => return,
33     };
34     for j in 0..4 {
35         match Some(j) {
36             Some(i) => i,
37             None => continue,
38         };
39         match Some(j) {
40             Some(i) => i,
41             None => break,
42         };
43     }
44
45     // cases where the none arm isn't a constant expression
46     // are not linted due to potential ownership issues
47
48     // ownership issue example, don't lint
49     struct NonCopyable;
50     let mut option: Option<NonCopyable> = None;
51     match option {
52         Some(x) => x,
53         None => {
54             option = Some(NonCopyable);
55             // some more code ...
56             option.unwrap()
57         },
58     };
59
60     // ownership issue example, don't lint
61     let option: Option<&str> = None;
62     match option {
63         Some(s) => s,
64         None => &format!("{} {}!", "hello", "world"),
65     };
66 }
67
68 fn main() {}