]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/needless_return.fixed
bump version to 1.80.1+dfsg1-1~bpo12+pve1
[rustc.git] / src / tools / clippy / tests / ui / needless_return.fixed
CommitLineData
064997fb 1#![feature(lint_reasons)]
9c376795 2#![feature(yeet_expr)]
cdc7bbd5
XL
3#![allow(unused)]
4#![allow(
5 clippy::if_same_then_else,
6 clippy::single_match,
c295e0f8 7 clippy::needless_bool,
fe692bf9
FG
8 clippy::equatable_if_let,
9 clippy::needless_else
cdc7bbd5 10)]
f20569fa
XL
11#![warn(clippy::needless_return)]
12
064997fb
FG
13use std::cell::RefCell;
14
f20569fa
XL
15macro_rules! the_answer {
16 () => {
17 42
18 };
19}
20
21fn test_end_of_fn() -> bool {
22 if true {
23 // no error!
24 return true;
25 }
26 true
27}
28
29fn test_no_semicolon() -> bool {
30 true
31}
32
9ffffee4
FG
33#[rustfmt::skip]
34fn test_multiple_semicolon() -> bool {
35 true
36}
37
38#[rustfmt::skip]
39fn test_multiple_semicolon_with_spaces() -> bool {
40 true
41}
42
f20569fa
XL
43fn test_if_block() -> bool {
44 if true {
45 true
46 } else {
47 false
48 }
49}
50
51fn test_match(x: bool) -> bool {
52 match x {
53 true => false,
54 false => {
55 true
56 },
57 }
58}
59
60fn test_closure() {
61 let _ = || {
62 true
63 };
64 let _ = || true;
65}
66
67fn test_macro_call() -> i32 {
923072b8 68 the_answer!()
f20569fa
XL
69}
70
71fn test_void_fun() {
f20569fa
XL
72}
73
74fn test_void_if_fun(b: bool) {
75 if b {
f20569fa 76 } else {
f20569fa
XL
77 }
78}
79
80fn test_void_match(x: u32) {
81 match x {
82 0 => (),
a2a8927a
XL
83 _ => (),
84 }
85}
86
87fn test_nested_match(x: u32) {
88 match x {
89 0 => (),
90 1 => {
91 let _ = 42;
a2a8927a
XL
92 },
93 _ => (),
f20569fa
XL
94 }
95}
96
064997fb
FG
97fn temporary_outlives_local() -> String {
98 let x = RefCell::<String>::default();
99 return x.borrow().clone();
f20569fa
XL
100}
101
102fn borrows_but_not_last(value: bool) -> String {
103 if value {
064997fb
FG
104 let x = RefCell::<String>::default();
105 let _a = x.borrow().clone();
f20569fa
XL
106 String::from("test")
107 } else {
108 String::new()
109 }
110}
111
112macro_rules! needed_return {
113 ($e:expr) => {
114 if $e > 3 {
115 return;
116 }
117 };
118}
119
120fn test_return_in_macro() {
121 // This will return and the macro below won't be executed. Removing the `return` from the macro
122 // will change semantics.
123 needed_return!(10);
124 needed_return!(0);
125}
126
127mod issue6501 {
a2a8927a 128 #[allow(clippy::unnecessary_lazy_evaluations)]
f20569fa
XL
129 fn foo(bar: Result<(), ()>) {
130 bar.unwrap_or_else(|_| {})
131 }
132
133 fn test_closure() {
134 let _ = || {
f20569fa
XL
135 };
136 let _ = || {};
137 }
138
139 struct Foo;
140 #[allow(clippy::unnecessary_lazy_evaluations)]
141 fn bar(res: Result<Foo, u8>) -> Foo {
142 res.unwrap_or_else(|_| Foo)
143 }
144}
145
cdc7bbd5
XL
146async fn async_test_end_of_fn() -> bool {
147 if true {
148 // no error!
149 return true;
150 }
151 true
152}
153
154async fn async_test_no_semicolon() -> bool {
155 true
156}
157
158async fn async_test_if_block() -> bool {
159 if true {
160 true
161 } else {
162 false
163 }
f20569fa 164}
cdc7bbd5
XL
165
166async fn async_test_match(x: bool) -> bool {
167 match x {
168 true => false,
169 false => {
170 true
171 },
172 }
173}
174
175async fn async_test_closure() {
176 let _ = || {
177 true
178 };
179 let _ = || true;
180}
181
182async fn async_test_macro_call() -> i32 {
923072b8 183 the_answer!()
cdc7bbd5
XL
184}
185
186async fn async_test_void_fun() {
cdc7bbd5
XL
187}
188
189async fn async_test_void_if_fun(b: bool) {
190 if b {
cdc7bbd5 191 } else {
cdc7bbd5
XL
192 }
193}
194
195async fn async_test_void_match(x: u32) {
196 match x {
197 0 => (),
a2a8927a 198 _ => (),
cdc7bbd5
XL
199 }
200}
201
064997fb
FG
202async fn async_temporary_outlives_local() -> String {
203 let x = RefCell::<String>::default();
204 return x.borrow().clone();
cdc7bbd5
XL
205}
206
207async fn async_borrows_but_not_last(value: bool) -> String {
208 if value {
064997fb
FG
209 let x = RefCell::<String>::default();
210 let _a = x.borrow().clone();
cdc7bbd5
XL
211 String::from("test")
212 } else {
213 String::new()
214 }
215}
216
217async fn async_test_return_in_macro() {
218 needed_return!(10);
219 needed_return!(0);
220}
221
c295e0f8
XL
222fn let_else() {
223 let Some(1) = Some(1) else { return };
224}
225
923072b8
FG
226fn needless_return_macro() -> String {
227 let _ = "foo";
228 let _ = "bar";
229 format!("Hello {}", "world!")
230}
231
f2b60f7d 232fn issue_9361() -> i32 {
49aad941
FG
233 let n = 1;
234 #[allow(clippy::arithmetic_side_effects)]
235 return n + n;
064997fb
FG
236}
237
2b03887a
FG
238fn issue8336(x: i32) -> bool {
239 if x > 0 {
240 println!("something");
241 true
242 } else {
243 false
244 }
245}
246
247fn issue8156(x: u8) -> u64 {
248 match x {
249 80 => {
250 10
251 },
252 _ => {
253 100
254 },
255 }
256}
257
258// Ideally the compiler should throw `unused_braces` in this case
259fn issue9192() -> i32 {
260 {
261 0
262 }
263}
264
265fn issue9503(x: usize) -> isize {
266 unsafe {
267 if x > 12 {
268 *(x as *const isize)
269 } else {
270 !*(x as *const isize)
271 }
272 }
273}
274
487cf647
FG
275mod issue9416 {
276 pub fn with_newline() {
277 let _ = 42;
278 }
279
280 #[rustfmt::skip]
281 pub fn oneline() {
282 let _ = 42;
283 }
284}
285
9c376795
FG
286fn issue9947() -> Result<(), String> {
287 do yeet "hello";
288}
289
290// without anyhow, but triggers the same bug I believe
291#[expect(clippy::useless_format)]
292fn issue10051() -> Result<String, String> {
293 if true {
294 Ok(format!("ok!"))
295 } else {
296 Err(format!("err!"))
297 }
298}
299
9ffffee4
FG
300mod issue10049 {
301 fn single() -> u32 {
302 if true { 1 } else { 2 }
303 }
304
305 fn multiple(b1: bool, b2: bool, b3: bool) -> u32 {
306 (if b1 { 0 } else { 1 } | if b2 { 2 } else { 3 } | if b3 { 4 } else { 5 })
307 }
308}
309
353b0b11
FG
310fn test_match_as_stmt() {
311 let x = 9;
312 match x {
313 1 => 2,
314 2 => return,
315 _ => 0,
316 };
317}
318
e8be2606
FG
319fn allow_works() -> i32 {
320 #[allow(clippy::needless_return, clippy::match_single_binding)]
321 match () {
322 () => return 42,
323 }
324}
325
31ef2f64
FG
326fn conjunctive_blocks() -> String {
327 ({ "a".to_string() } + "b" + { "c" })
328}
329
cdc7bbd5 330fn main() {}