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