]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_unwrap_or.rs
Auto merge of #6167 - ThibsG:IdenticalArgumentsAssertEq3574, r=ebroto
[rust.git] / tests / ui / manual_unwrap_or.rs
1 // run-rustfix
2 #![allow(dead_code)]
3
4 fn 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 main() {}