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