3 #![warn(clippy::needless_question_mark)]
4 #![allow(clippy::needless_return, clippy::unnecessary_unwrap, dead_code, unused_must_use)]
5 #![feature(custom_inner_attributes)]
12 magic: Result<usize, bool>,
15 fn simple_option_bad1(to: TO) -> Option<usize> {
16 // return as a statement
17 return Some(to.magic?);
20 // formatting will add a semi-colon, which would make
21 // this identical to the test case above
23 fn simple_option_bad2(to: TO) -> Option<usize> {
24 // return as an expression
25 return Some(to.magic?)
28 fn simple_option_bad3(to: TO) -> Option<usize> {
29 // block value "return"
33 fn simple_option_bad4(to: Option<TO>) -> Option<usize> {
34 // single line closure
35 to.and_then(|t| Some(t.magic?))
38 // formatting this will remove the block brackets, making
39 // this test identical to the one above
41 fn simple_option_bad5(to: Option<TO>) -> Option<usize> {
48 fn simple_result_bad1(tr: TR) -> Result<usize, bool> {
52 // formatting will add a semi-colon, which would make
53 // this identical to the test case above
55 fn simple_result_bad2(tr: TR) -> Result<usize, bool> {
59 fn simple_result_bad3(tr: TR) -> Result<usize, bool> {
63 fn simple_result_bad4(tr: Result<TR, bool>) -> Result<usize, bool> {
64 tr.and_then(|t| Ok(t.magic?))
67 // formatting this will remove the block brackets, making
68 // this test identical to the one above
70 fn simple_result_bad5(tr: Result<TR, bool>) -> Result<usize, bool> {
76 fn also_bad(tr: Result<TR, bool>) -> Result<usize, bool> {
84 fn false_positive_test<U, T>(x: Result<(), U>) -> Result<(), T>
93 mod question_mark_none {
94 #![clippy::msrv = "1.12.0"]
95 fn needless_question_mark_option() -> Option<usize> {
99 let to = TO { magic: None };
100 Some(to.magic?) // should not be triggered
103 fn needless_question_mark_result() -> Result<usize, bool> {
105 magic: Result<usize, bool>,
107 let to = TO { magic: Ok(1_usize) };
108 Ok(to.magic?) // should not be triggered
112 needless_question_mark_option();
113 needless_question_mark_result();
117 mod question_mark_result {
118 #![clippy::msrv = "1.21.0"]
119 fn needless_question_mark_option() -> Option<usize> {
121 magic: Option<usize>,
123 let to = TO { magic: None };
124 Some(to.magic?) // should not be triggered
127 fn needless_question_mark_result() -> Result<usize, bool> {
129 magic: Result<usize, bool>,
131 let to = TO { magic: Ok(1_usize) };
132 Ok(to.magic?) // should be triggered
136 needless_question_mark_option();
137 needless_question_mark_result();
141 mod question_mark_both {
142 #![clippy::msrv = "1.22.0"]
143 fn needless_question_mark_option() -> Option<usize> {
145 magic: Option<usize>,
147 let to = TO { magic: None };
148 Some(to.magic?) // should be triggered
151 fn needless_question_mark_result() -> Result<usize, bool> {
153 magic: Result<usize, bool>,
155 let to = TO { magic: Ok(1_usize) };
156 Ok(to.magic?) // should be triggered
160 needless_question_mark_option();
161 needless_question_mark_result();