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