1 // run-rustfix
2 #![allow(dead_code)]
3 #![allow(unused_variables, clippy::unnecessary_wraps)]
4 
option_unwrap_or()5 fn option_unwrap_or() {
6     // int case
7     match Some(1) {
8         Some(i) => i,
9         None => 42,
10     };
11 
12     // int case reversed
13     match Some(1) {
14         None => 42,
15         Some(i) => i,
16     };
17 
18     // richer none expr
19     match Some(1) {
20         Some(i) => i,
21         None => 1 + 42,
22     };
23 
24     // multiline case
25     #[rustfmt::skip]
26     match Some(1) {
27         Some(i) => i,
28         None => {
29             42 + 42
30                 + 42 + 42 + 42
31                 + 42 + 42 + 42
32         }
33     };
34 
35     // string case
36     match Some("Bob") {
37         Some(i) => i,
38         None => "Alice",
39     };
40 
41     // don't lint
42     match Some(1) {
43         Some(i) => i + 2,
44         None => 42,
45     };
46     match Some(1) {
47         Some(i) => i,
48         None => return,
49     };
50     for j in 0..4 {
51         match Some(j) {
52             Some(i) => i,
53             None => continue,
54         };
55         match Some(j) {
56             Some(i) => i,
57             None => break,
58         };
59     }
60 
61     // cases where the none arm isn't a constant expression
62     // are not linted due to potential ownership issues
63 
64     // ownership issue example, don't lint
65     struct NonCopyable;
66     let mut option: Option<NonCopyable> = None;
67     match option {
68         Some(x) => x,
69         None => {
70             option = Some(NonCopyable);
71             // some more code ...
72             option.unwrap()
73         },
74     };
75 
76     // ownership issue example, don't lint
77     let option: Option<&str> = None;
78     match option {
79         Some(s) => s,
80         None => &format!("{} {}!", "hello", "world"),
81     };
82 }
83 
result_unwrap_or()84 fn result_unwrap_or() {
85     // int case
86     match Ok::<i32, &str>(1) {
87         Ok(i) => i,
88         Err(_) => 42,
89     };
90 
91     // int case, scrutinee is a binding
92     let a = Ok::<i32, &str>(1);
93     match a {
94         Ok(i) => i,
95         Err(_) => 42,
96     };
97 
98     // int case, suggestion must surround Result expr with parentheses
99     match Ok(1) as Result<i32, &str> {
100         Ok(i) => i,
101         Err(_) => 42,
102     };
103 
104     // method call case, suggestion must not surround Result expr `s.method()` with parentheses
105     struct S {}
106     impl S {
107         fn method(self) -> Option<i32> {
108             Some(42)
109         }
110     }
111     let s = S {};
112     match s.method() {
113         Some(i) => i,
114         None => 42,
115     };
116 
117     // int case reversed
118     match Ok::<i32, &str>(1) {
119         Err(_) => 42,
120         Ok(i) => i,
121     };
122 
123     // richer none expr
124     match Ok::<i32, &str>(1) {
125         Ok(i) => i,
126         Err(_) => 1 + 42,
127     };
128 
129     // multiline case
130     #[rustfmt::skip]
131     match Ok::<i32, &str>(1) {
132         Ok(i) => i,
133         Err(_) => {
134             42 + 42
135                 + 42 + 42 + 42
136                 + 42 + 42 + 42
137         }
138     };
139 
140     // string case
141     match Ok::<&str, &str>("Bob") {
142         Ok(i) => i,
143         Err(_) => "Alice",
144     };
145 
146     // don't lint
147     match Ok::<i32, &str>(1) {
148         Ok(i) => i + 2,
149         Err(_) => 42,
150     };
151     match Ok::<i32, &str>(1) {
152         Ok(i) => i,
153         Err(_) => return,
154     };
155     for j in 0..4 {
156         match Ok::<i32, &str>(j) {
157             Ok(i) => i,
158             Err(_) => continue,
159         };
160         match Ok::<i32, &str>(j) {
161             Ok(i) => i,
162             Err(_) => break,
163         };
164     }
165 
166     // don't lint, Err value is used
167     match Ok::<&str, &str>("Alice") {
168         Ok(s) => s,
169         Err(s) => s,
170     };
171     // could lint, but unused_variables takes care of it
172     match Ok::<&str, &str>("Alice") {
173         Ok(s) => s,
174         Err(s) => "Bob",
175     };
176 }
177 
178 // don't lint in const fn
const_fn_option_unwrap_or()179 const fn const_fn_option_unwrap_or() {
180     match Some(1) {
181         Some(s) => s,
182         None => 0,
183     };
184 }
185 
const_fn_result_unwrap_or()186 const fn const_fn_result_unwrap_or() {
187     match Ok::<&str, &str>("Alice") {
188         Ok(s) => s,
189         Err(_) => "Bob",
190     };
191 }
192 
193 mod issue6965 {
194     macro_rules! some_macro {
195         () => {
196             if 1 > 2 { Some(1) } else { None }
197         };
198     }
199 
test()200     fn test() {
201         let _ = match some_macro!() {
202             Some(val) => val,
203             None => 0,
204         };
205     }
206 }
207 
208 use std::rc::Rc;
format_name(name: Option<&Rc<str>>) -> &str209 fn format_name(name: Option<&Rc<str>>) -> &str {
210     match name {
211         None => "<anon>",
212         Some(name) => name,
213     }
214 }
215 
implicit_deref_ref()216 fn implicit_deref_ref() {
217     let _: &str = match Some(&"bye") {
218         None => "hi",
219         Some(s) => s,
220     };
221 }
222 
main()223 fn main() {}
224