]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/question_mark.fixed
Rollup merge of #84221 - ABouttefeux:generic-arg-elision, r=estebank
[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 main() {
108     some_func(Some(42));
109     some_func(None);
110     some_other_func(Some(42));
111
112     let copy_struct = CopyStruct { opt: Some(54) };
113     copy_struct.func();
114
115     let move_struct = MoveStruct {
116         opt: Some(vec![42, 1337]),
117     };
118     move_struct.ref_func();
119     move_struct.clone().mov_func_reuse();
120     move_struct.mov_func_no_use();
121
122     let so = SeemsOption::Some(45);
123     returns_something_similar_to_option(so);
124
125     func();
126 }