]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_unwrap_or.rs
beca1de0ed1651861e9107fdfed16a9cc23bea8f
[rust.git] / tests / ui / manual_unwrap_or.rs
1 // run-rustfix
2 #![allow(dead_code)]
3
4 fn option_unwrap_or() {
5     // int case
6     match Some(1) {
7         Some(i) => i,
8         None => 42,
9     };
10
11     // int case reversed
12     match Some(1) {
13         None => 42,
14         Some(i) => i,
15     };
16
17     // richer none expr
18     match Some(1) {
19         Some(i) => i,
20         None => 1 + 42,
21     };
22
23     // multiline case
24     #[rustfmt::skip]
25     match Some(1) {
26         Some(i) => i,
27         None => {
28             42 + 42
29                 + 42 + 42 + 42
30                 + 42 + 42 + 42
31         }
32     };
33
34     // string case
35     match Some("Bob") {
36         Some(i) => i,
37         None => "Alice",
38     };
39
40     // don't lint
41     match Some(1) {
42         Some(i) => i + 2,
43         None => 42,
44     };
45     match Some(1) {
46         Some(i) => i,
47         None => return,
48     };
49     for j in 0..4 {
50         match Some(j) {
51             Some(i) => i,
52             None => continue,
53         };
54         match Some(j) {
55             Some(i) => i,
56             None => break,
57         };
58     }
59
60     // cases where the none arm isn't a constant expression
61     // are not linted due to potential ownership issues
62
63     // ownership issue example, don't lint
64     struct NonCopyable;
65     let mut option: Option<NonCopyable> = None;
66     match option {
67         Some(x) => x,
68         None => {
69             option = Some(NonCopyable);
70             // some more code ...
71             option.unwrap()
72         },
73     };
74
75     // ownership issue example, don't lint
76     let option: Option<&str> = None;
77     match option {
78         Some(s) => s,
79         None => &format!("{} {}!", "hello", "world"),
80     };
81 }
82
83 fn result_unwrap_or() {
84     // int case
85     match Ok(1) as Result<i32, &str> {
86         Ok(i) => i,
87         Err(_) => 42,
88     };
89
90     // int case reversed
91     match Ok(1) as Result<i32, &str> {
92         Err(_) => 42,
93         Ok(i) => i,
94     };
95
96     // richer none expr
97     match Ok(1) as Result<i32, &str> {
98         Ok(i) => i,
99         Err(_) => 1 + 42,
100     };
101
102     // multiline case
103     #[rustfmt::skip]
104     match Ok(1) as Result<i32, &str> {
105         Ok(i) => i,
106         Err(_) => {
107             42 + 42
108                 + 42 + 42 + 42
109                 + 42 + 42 + 42
110         }
111     };
112
113     // string case
114     match Ok("Bob") as Result<&str, &str> {
115         Ok(i) => i,
116         Err(_) => "Alice",
117     };
118
119     // don't lint
120     match Ok(1) as Result<i32, &str> {
121         Ok(i) => i + 2,
122         Err(_) => 42,
123     };
124     match Ok(1) as Result<i32, &str> {
125         Ok(i) => i,
126         Err(_) => return,
127     };
128     for j in 0..4 {
129         match Ok(j) as Result<i32, &str> {
130             Ok(i) => i,
131             Err(_) => continue,
132         };
133         match Ok(j) as Result<i32, &str> {
134             Ok(i) => i,
135             Err(_) => break,
136         };
137     }
138 }
139
140 fn main() {}