]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_unwrap_or.rs
manual-unwrap-or / pr remarks
[rust.git] / tests / ui / manual_unwrap_or.rs
1 // run-rustfix
2 #![allow(dead_code)]
3 #![allow(unused_variables)]
4
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
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, suggestion must surround with parenthesis
92     match Ok(1) as Result<i32, &str> {
93         Ok(i) => i,
94         Err(_) => 42,
95     };
96
97     // int case reversed
98     match Ok::<i32, &str>(1) {
99         Err(_) => 42,
100         Ok(i) => i,
101     };
102
103     // richer none expr
104     match Ok::<i32, &str>(1) {
105         Ok(i) => i,
106         Err(_) => 1 + 42,
107     };
108
109     // multiline case
110     #[rustfmt::skip]
111     match Ok::<i32, &str>(1) {
112         Ok(i) => i,
113         Err(_) => {
114             42 + 42
115                 + 42 + 42 + 42
116                 + 42 + 42 + 42
117         }
118     };
119
120     // string case
121     match Ok::<&str, &str>("Bob") {
122         Ok(i) => i,
123         Err(_) => "Alice",
124     };
125
126     // don't lint
127     match Ok::<i32, &str>(1) {
128         Ok(i) => i + 2,
129         Err(_) => 42,
130     };
131     match Ok::<i32, &str>(1) {
132         Ok(i) => i,
133         Err(_) => return,
134     };
135     for j in 0..4 {
136         match Ok::<i32, &str>(j) {
137             Ok(i) => i,
138             Err(_) => continue,
139         };
140         match Ok::<i32, &str>(j) {
141             Ok(i) => i,
142             Err(_) => break,
143         };
144     }
145
146     // don't lint, Err value is used
147     match Ok::<&str, &str>("Alice") {
148         Ok(s) => s,
149         Err(s) => s,
150     };
151     // could lint, but unused_variables takes care of it
152     match Ok::<&str, &str>("Alice") {
153         Ok(s) => s,
154         Err(s) => "Bob",
155     };
156 }
157
158 fn main() {}