]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/needless_question_mark.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / needless_question_mark.rs
CommitLineData
f20569fa
XL
1// run-rustfix
2
3#![warn(clippy::needless_question_mark)]
4#![allow(
5 clippy::needless_return,
6 clippy::unnecessary_unwrap,
7 clippy::upper_case_acronyms,
8 dead_code,
9 unused_must_use
10)]
11#![feature(custom_inner_attributes)]
12
13struct TO {
14 magic: Option<usize>,
15}
16
17struct TR {
18 magic: Result<usize, bool>,
19}
20
21fn simple_option_bad1(to: TO) -> Option<usize> {
22 // return as a statement
23 return Some(to.magic?);
24}
25
26// formatting will add a semi-colon, which would make
27// this identical to the test case above
28#[rustfmt::skip]
29fn simple_option_bad2(to: TO) -> Option<usize> {
30 // return as an expression
31 return Some(to.magic?)
32}
33
34fn simple_option_bad3(to: TO) -> Option<usize> {
35 // block value "return"
36 Some(to.magic?)
37}
38
39fn simple_option_bad4(to: Option<TO>) -> Option<usize> {
40 // single line closure
41 to.and_then(|t| Some(t.magic?))
42}
43
44// formatting this will remove the block brackets, making
45// this test identical to the one above
46#[rustfmt::skip]
47fn simple_option_bad5(to: Option<TO>) -> Option<usize> {
48 // closure with body
49 to.and_then(|t| {
50 Some(t.magic?)
51 })
52}
53
54fn simple_result_bad1(tr: TR) -> Result<usize, bool> {
55 return Ok(tr.magic?);
56}
57
58// formatting will add a semi-colon, which would make
59// this identical to the test case above
60#[rustfmt::skip]
61fn simple_result_bad2(tr: TR) -> Result<usize, bool> {
62 return Ok(tr.magic?)
63}
64
65fn simple_result_bad3(tr: TR) -> Result<usize, bool> {
66 Ok(tr.magic?)
67}
68
69fn simple_result_bad4(tr: Result<TR, bool>) -> Result<usize, bool> {
70 tr.and_then(|t| Ok(t.magic?))
71}
72
73// formatting this will remove the block brackets, making
74// this test identical to the one above
75#[rustfmt::skip]
76fn simple_result_bad5(tr: Result<TR, bool>) -> Result<usize, bool> {
77 tr.and_then(|t| {
78 Ok(t.magic?)
79 })
80}
81
82fn also_bad(tr: Result<TR, bool>) -> Result<usize, bool> {
83 if tr.is_ok() {
84 let t = tr.unwrap();
85 return Ok(t.magic?);
86 }
87 Err(false)
88}
89
90fn false_positive_test<U, T>(x: Result<(), U>) -> Result<(), T>
91where
92 T: From<U>,
93{
94 Ok(x?)
95}
96
97fn main() {}
98
99mod question_mark_none {
100 #![clippy::msrv = "1.12.0"]
101 fn needless_question_mark_option() -> Option<usize> {
102 struct TO {
103 magic: Option<usize>,
104 }
105 let to = TO { magic: None };
106 Some(to.magic?) // should not be triggered
107 }
108
109 fn needless_question_mark_result() -> Result<usize, bool> {
110 struct TO {
111 magic: Result<usize, bool>,
112 }
113 let to = TO { magic: Ok(1_usize) };
114 Ok(to.magic?) // should not be triggered
115 }
116
117 fn main() {
118 needless_question_mark_option();
119 needless_question_mark_result();
120 }
121}
122
123mod question_mark_result {
124 #![clippy::msrv = "1.21.0"]
125 fn needless_question_mark_option() -> Option<usize> {
126 struct TO {
127 magic: Option<usize>,
128 }
129 let to = TO { magic: None };
130 Some(to.magic?) // should not be triggered
131 }
132
133 fn needless_question_mark_result() -> Result<usize, bool> {
134 struct TO {
135 magic: Result<usize, bool>,
136 }
137 let to = TO { magic: Ok(1_usize) };
138 Ok(to.magic?) // should be triggered
139 }
140
141 fn main() {
142 needless_question_mark_option();
143 needless_question_mark_result();
144 }
145}
146
147mod question_mark_both {
148 #![clippy::msrv = "1.22.0"]
149 fn needless_question_mark_option() -> Option<usize> {
150 struct TO {
151 magic: Option<usize>,
152 }
153 let to = TO { magic: None };
154 Some(to.magic?) // should be triggered
155 }
156
157 fn needless_question_mark_result() -> Result<usize, bool> {
158 struct TO {
159 magic: Result<usize, bool>,
160 }
161 let to = TO { magic: Ok(1_usize) };
162 Ok(to.magic?) // should be triggered
163 }
164
165 fn main() {
166 needless_question_mark_option();
167 needless_question_mark_result();
168 }
169}