]> git.lizzy.rs Git - rust.git/blob - tests/ui/question_mark.fixed
Update `question_mark` expected test output
[rust.git] / tests / ui / question_mark.fixed
1 // run-rustfix
2 #![allow(unreachable_code)]
3 #![allow(clippy::unnecessary_wraps)]
4
5 use std::env;
6 use std::path::PathBuf;
7
8 fn some_func(a: Option<u32>) -> Option<u32> {
9     a?;
10
11     a
12 }
13
14 fn some_other_func(a: Option<u32>) -> Option<u32> {
15     if a.is_none() {
16         return None;
17     } else {
18         return Some(0);
19     }
20     unreachable!()
21 }
22
23 pub enum SeemsOption<T> {
24     Some(T),
25     None,
26 }
27
28 impl<T> SeemsOption<T> {
29     pub fn is_none(&self) -> bool {
30         match *self {
31             SeemsOption::None => true,
32             SeemsOption::Some(_) => false,
33         }
34     }
35 }
36
37 fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
38     if a.is_none() {
39         return SeemsOption::None;
40     }
41
42     a
43 }
44
45 pub struct CopyStruct {
46     pub opt: Option<u32>,
47 }
48
49 impl CopyStruct {
50     #[rustfmt::skip]
51     pub fn func(&self) -> Option<u32> {
52         (self.opt)?;
53
54         self.opt?;
55
56         let _ = Some(self.opt?);
57
58         let _ = self.opt?;
59
60         self.opt
61     }
62 }
63
64 #[derive(Clone)]
65 pub struct MoveStruct {
66     pub opt: Option<Vec<u32>>,
67 }
68
69 impl MoveStruct {
70     pub fn ref_func(&self) -> Option<Vec<u32>> {
71         self.opt.as_ref()?;
72
73         self.opt.clone()
74     }
75
76     pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
77         self.opt.as_ref()?;
78
79         self.opt
80     }
81
82     pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
83         self.opt.as_ref()?;
84         Some(Vec::new())
85     }
86
87     pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
88         let v: &Vec<_> = self.opt.as_ref()?;
89
90         Some(v.clone())
91     }
92
93     pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
94         let v = self.opt?;
95
96         Some(v)
97     }
98 }
99
100 fn func() -> Option<i32> {
101     fn f() -> Option<String> {
102         Some(String::new())
103     }
104
105     f()?;
106
107     Some(0)
108 }
109
110 fn func_returning_result() -> Result<i32, String> {
111     Ok(1)
112 }
113
114 fn result_func(x: Result<i32, String>) -> Result<i32, String> {
115     let _ = x?;
116
117     x.as_ref()?;
118
119     // No warning
120     let y = if let Ok(x) = x {
121         x
122     } else {
123         return Err("some error".to_string());
124     };
125
126     // issue #7859
127     // no warning
128     let _ = if let Ok(x) = func_returning_result() {
129         x
130     } else {
131         return Err("some error".to_string());
132     };
133
134     if func_returning_result().is_err() {
135         return func_returning_result();
136     }
137
138     Ok(y)
139 }
140
141 fn main() {
142     some_func(Some(42));
143     some_func(None);
144     some_other_func(Some(42));
145
146     let copy_struct = CopyStruct { opt: Some(54) };
147     copy_struct.func();
148
149     let move_struct = MoveStruct {
150         opt: Some(vec![42, 1337]),
151     };
152     move_struct.ref_func();
153     move_struct.clone().mov_func_reuse();
154     move_struct.mov_func_no_use();
155
156     let so = SeemsOption::Some(45);
157     returns_something_similar_to_option(so);
158
159     func();
160
161     let _ = result_func(Ok(42));
162 }