]> git.lizzy.rs Git - rust.git/blob - tests/ui/question_mark.rs
Auto merge of #8374 - Alexendoo:bless-revisions, r=camsteffen
[rust.git] / tests / ui / question_mark.rs
1 // run-rustfix
2 #![allow(unreachable_code)]
3 #![allow(clippy::unnecessary_wraps)]
4
5 fn some_func(a: Option<u32>) -> Option<u32> {
6     if a.is_none() {
7         return None;
8     }
9
10     a
11 }
12
13 fn some_other_func(a: Option<u32>) -> Option<u32> {
14     if a.is_none() {
15         return None;
16     } else {
17         return Some(0);
18     }
19     unreachable!()
20 }
21
22 pub enum SeemsOption<T> {
23     Some(T),
24     None,
25 }
26
27 impl<T> SeemsOption<T> {
28     pub fn is_none(&self) -> bool {
29         match *self {
30             SeemsOption::None => true,
31             SeemsOption::Some(_) => false,
32         }
33     }
34 }
35
36 fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
37     if a.is_none() {
38         return SeemsOption::None;
39     }
40
41     a
42 }
43
44 pub struct CopyStruct {
45     pub opt: Option<u32>,
46 }
47
48 impl CopyStruct {
49     #[rustfmt::skip]
50     pub fn func(&self) -> Option<u32> {
51         if (self.opt).is_none() {
52             return None;
53         }
54
55         if self.opt.is_none() {
56             return None
57         }
58
59         let _ = if self.opt.is_none() {
60             return None;
61         } else {
62             self.opt
63         };
64
65         let _ = if let Some(x) = self.opt {
66             x
67         } else {
68             return None;
69         };
70
71         self.opt
72     }
73 }
74
75 #[derive(Clone)]
76 pub struct MoveStruct {
77     pub opt: Option<Vec<u32>>,
78 }
79
80 impl MoveStruct {
81     pub fn ref_func(&self) -> Option<Vec<u32>> {
82         if self.opt.is_none() {
83             return None;
84         }
85
86         self.opt.clone()
87     }
88
89     pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
90         if self.opt.is_none() {
91             return None;
92         }
93
94         self.opt
95     }
96
97     pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
98         if self.opt.is_none() {
99             return None;
100         }
101         Some(Vec::new())
102     }
103
104     pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
105         let v: &Vec<_> = if let Some(ref v) = self.opt {
106             v
107         } else {
108             return None;
109         };
110
111         Some(v.clone())
112     }
113
114     pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
115         let v = if let Some(v) = self.opt {
116             v
117         } else {
118             return None;
119         };
120
121         Some(v)
122     }
123 }
124
125 fn func() -> Option<i32> {
126     fn f() -> Option<String> {
127         Some(String::new())
128     }
129
130     if f().is_none() {
131         return None;
132     }
133
134     Some(0)
135 }
136
137 fn func_returning_result() -> Result<i32, i32> {
138     Ok(1)
139 }
140
141 fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
142     let _ = if let Ok(x) = x { x } else { return x };
143
144     if x.is_err() {
145         return x;
146     }
147
148     // No warning
149     let y = if let Ok(x) = x {
150         x
151     } else {
152         return Err(0);
153     };
154
155     // issue #7859
156     // no warning
157     let _ = if let Ok(x) = func_returning_result() {
158         x
159     } else {
160         return Err(0);
161     };
162
163     // no warning
164     if func_returning_result().is_err() {
165         return func_returning_result();
166     }
167
168     Ok(y)
169 }
170
171 // see issue #8019
172 pub enum NotOption {
173     None,
174     First,
175     AfterFirst,
176 }
177
178 fn obj(_: i32) -> Result<(), NotOption> {
179     Err(NotOption::First)
180 }
181
182 fn f() -> NotOption {
183     if obj(2).is_err() {
184         return NotOption::None;
185     }
186     NotOption::First
187 }
188
189 fn main() {
190     some_func(Some(42));
191     some_func(None);
192     some_other_func(Some(42));
193
194     let copy_struct = CopyStruct { opt: Some(54) };
195     copy_struct.func();
196
197     let move_struct = MoveStruct {
198         opt: Some(vec![42, 1337]),
199     };
200     move_struct.ref_func();
201     move_struct.clone().mov_func_reuse();
202     move_struct.mov_func_no_use();
203
204     let so = SeemsOption::Some(45);
205     returns_something_similar_to_option(so);
206
207     func();
208
209     let _ = result_func(Ok(42));
210     let _ = f();
211 }