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