]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/needless_question_mark.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / needless_question_mark.rs
1 #![warn(clippy::needless_question_mark)]
2 #![allow(
3 clippy::needless_return,
4 clippy::unnecessary_unwrap,
5 clippy::upper_case_acronyms,
6 dead_code,
7 unused_must_use
8 )]
9
10 struct TO {
11 magic: Option<usize>,
12 }
13
14 struct TR {
15 magic: Result<usize, bool>,
16 }
17
18 fn simple_option_bad1(to: TO) -> Option<usize> {
19 // return as a statement
20 return Some(to.magic?);
21 }
22
23 // formatting will add a semi-colon, which would make
24 // this identical to the test case above
25 #[rustfmt::skip]
26 fn simple_option_bad2(to: TO) -> Option<usize> {
27 // return as an expression
28 return Some(to.magic?)
29 }
30
31 fn simple_option_bad3(to: TO) -> Option<usize> {
32 // block value "return"
33 Some(to.magic?)
34 }
35
36 fn simple_option_bad4(to: Option<TO>) -> Option<usize> {
37 // single line closure
38 to.and_then(|t| Some(t.magic?))
39 }
40
41 // formatting this will remove the block brackets, making
42 // this test identical to the one above
43 #[rustfmt::skip]
44 fn simple_option_bad5(to: Option<TO>) -> Option<usize> {
45 // closure with body
46 to.and_then(|t| {
47 Some(t.magic?)
48 })
49 }
50
51 fn simple_result_bad1(tr: TR) -> Result<usize, bool> {
52 return Ok(tr.magic?);
53 }
54
55 // formatting will add a semi-colon, which would make
56 // this identical to the test case above
57 #[rustfmt::skip]
58 fn simple_result_bad2(tr: TR) -> Result<usize, bool> {
59 return Ok(tr.magic?)
60 }
61
62 fn simple_result_bad3(tr: TR) -> Result<usize, bool> {
63 Ok(tr.magic?)
64 }
65
66 fn simple_result_bad4(tr: Result<TR, bool>) -> Result<usize, bool> {
67 tr.and_then(|t| Ok(t.magic?))
68 }
69
70 // formatting this will remove the block brackets, making
71 // this test identical to the one above
72 #[rustfmt::skip]
73 fn simple_result_bad5(tr: Result<TR, bool>) -> Result<usize, bool> {
74 tr.and_then(|t| {
75 Ok(t.magic?)
76 })
77 }
78
79 fn also_bad(tr: Result<TR, bool>) -> Result<usize, bool> {
80 if tr.is_ok() {
81 let t = tr.unwrap();
82 return Ok(t.magic?);
83 }
84 Err(false)
85 }
86
87 fn false_positive_test<U, T>(x: Result<(), U>) -> Result<(), T>
88 where
89 T: From<U>,
90 {
91 Ok(x?)
92 }
93
94 // not quite needless
95 fn deref_ref(s: Option<&String>) -> Option<&str> {
96 Some(s?)
97 }
98
99 fn main() {}
100
101 // #6921 if a macro wraps an expr in Some( ) and the ? is in the macro use,
102 // the suggestion fails to apply; do not lint
103 macro_rules! some_in_macro {
104 ($expr:expr) => {
105 || -> _ { Some($expr) }()
106 };
107 }
108
109 pub fn test1() {
110 let x = Some(3);
111 let _x = some_in_macro!(x?);
112 }
113
114 // this one is ok because both the ? and the Some are both inside the macro def
115 macro_rules! some_and_qmark_in_macro {
116 ($expr:expr) => {
117 || -> Option<_> { Some(Some($expr)?) }()
118 };
119 }
120
121 pub fn test2() {
122 let x = Some(3);
123 let _x = some_and_qmark_in_macro!(x?);
124 }
125
126 async fn async_option_bad(to: TO) -> Option<usize> {
127 let _ = Some(3);
128 Some(to.magic?)
129 }
130
131 async fn async_deref_ref(s: Option<&String>) -> Option<&str> {
132 Some(s?)
133 }
134
135 async fn async_result_bad(s: TR) -> Result<usize, bool> {
136 Ok(s.magic?)
137 }