]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_unwrap_or.fixed
ceb8985d3d5142c340f1e88fccb595d370af7dcd
[rust.git] / tests / ui / manual_unwrap_or.fixed
1 // run-rustfix
2 #![allow(dead_code)]
3
4 fn option_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 result_unwrap_or() {
69     // int case
70     (Ok(1) as Result<i32, &str>).unwrap_or(42);
71
72     // int case reversed
73     (Ok(1) as Result<i32, &str>).unwrap_or(42);
74
75     // richer none expr
76     (Ok(1) as Result<i32, &str>).unwrap_or(1 + 42);
77
78     // multiline case
79     #[rustfmt::skip]
80     (Ok(1) as Result<i32, &str>).unwrap_or({
81         42 + 42
82             + 42 + 42 + 42
83             + 42 + 42 + 42
84     });
85
86     // string case
87     (Ok("Bob") as Result<&str, &str>).unwrap_or("Alice");
88
89     // don't lint
90     match Ok(1) as Result<i32, &str> {
91         Ok(i) => i + 2,
92         Err(_) => 42,
93     };
94     match Ok(1) as Result<i32, &str> {
95         Ok(i) => i,
96         Err(_) => return,
97     };
98     for j in 0..4 {
99         match Ok(j) as Result<i32, &str> {
100             Ok(i) => i,
101             Err(_) => continue,
102         };
103         match Ok(j) as Result<i32, &str> {
104             Ok(i) => i,
105             Err(_) => break,
106         };
107     }
108 }
109
110 fn main() {}