]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_return.rs
Rollup merge of #105846 - compiler-errors:issue-105838, r=jackh726
[rust.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 fn test_if_block() -> bool {
35     if true {
36         return true;
37     } else {
38         return false;
39     }
40 }
41
42 fn test_match(x: bool) -> bool {
43     match x {
44         true => return false,
45         false => {
46             return true;
47         },
48     }
49 }
50
51 fn test_closure() {
52     let _ = || {
53         return true;
54     };
55     let _ = || return true;
56 }
57
58 fn test_macro_call() -> i32 {
59     return the_answer!();
60 }
61
62 fn test_void_fun() {
63     return;
64 }
65
66 fn test_void_if_fun(b: bool) {
67     if b {
68         return;
69     } else {
70         return;
71     }
72 }
73
74 fn test_void_match(x: u32) {
75     match x {
76         0 => (),
77         _ => return,
78     }
79 }
80
81 fn test_nested_match(x: u32) {
82     match x {
83         0 => (),
84         1 => {
85             let _ = 42;
86             return;
87         },
88         _ => return,
89     }
90 }
91
92 fn temporary_outlives_local() -> String {
93     let x = RefCell::<String>::default();
94     return x.borrow().clone();
95 }
96
97 fn borrows_but_not_last(value: bool) -> String {
98     if value {
99         let x = RefCell::<String>::default();
100         let _a = x.borrow().clone();
101         return String::from("test");
102     } else {
103         return String::new();
104     }
105 }
106
107 macro_rules! needed_return {
108     ($e:expr) => {
109         if $e > 3 {
110             return;
111         }
112     };
113 }
114
115 fn test_return_in_macro() {
116     // This will return and the macro below won't be executed. Removing the `return` from the macro
117     // will change semantics.
118     needed_return!(10);
119     needed_return!(0);
120 }
121
122 mod issue6501 {
123     #[allow(clippy::unnecessary_lazy_evaluations)]
124     fn foo(bar: Result<(), ()>) {
125         bar.unwrap_or_else(|_| return)
126     }
127
128     fn test_closure() {
129         let _ = || {
130             return;
131         };
132         let _ = || return;
133     }
134
135     struct Foo;
136     #[allow(clippy::unnecessary_lazy_evaluations)]
137     fn bar(res: Result<Foo, u8>) -> Foo {
138         res.unwrap_or_else(|_| return Foo)
139     }
140 }
141
142 async fn async_test_end_of_fn() -> bool {
143     if true {
144         // no error!
145         return true;
146     }
147     return true;
148 }
149
150 async fn async_test_no_semicolon() -> bool {
151     return true;
152 }
153
154 async fn async_test_if_block() -> bool {
155     if true {
156         return true;
157     } else {
158         return false;
159     }
160 }
161
162 async fn async_test_match(x: bool) -> bool {
163     match x {
164         true => return false,
165         false => {
166             return true;
167         },
168     }
169 }
170
171 async fn async_test_closure() {
172     let _ = || {
173         return true;
174     };
175     let _ = || return true;
176 }
177
178 async fn async_test_macro_call() -> i32 {
179     return the_answer!();
180 }
181
182 async fn async_test_void_fun() {
183     return;
184 }
185
186 async fn async_test_void_if_fun(b: bool) {
187     if b {
188         return;
189     } else {
190         return;
191     }
192 }
193
194 async fn async_test_void_match(x: u32) {
195     match x {
196         0 => (),
197         _ => return,
198     }
199 }
200
201 async fn async_temporary_outlives_local() -> String {
202     let x = RefCell::<String>::default();
203     return x.borrow().clone();
204 }
205
206 async fn async_borrows_but_not_last(value: bool) -> String {
207     if value {
208         let x = RefCell::<String>::default();
209         let _a = x.borrow().clone();
210         return String::from("test");
211     } else {
212         return String::new();
213     }
214 }
215
216 async fn async_test_return_in_macro() {
217     needed_return!(10);
218     needed_return!(0);
219 }
220
221 fn let_else() {
222     let Some(1) = Some(1) else { return };
223 }
224
225 fn needless_return_macro() -> String {
226     let _ = "foo";
227     let _ = "bar";
228     return format!("Hello {}", "world!");
229 }
230
231 fn issue_9361() -> i32 {
232     #[allow(clippy::integer_arithmetic)]
233     return 1 + 2;
234 }
235
236 fn issue8336(x: i32) -> bool {
237     if x > 0 {
238         println!("something");
239         return true;
240     } else {
241         return false;
242     };
243 }
244
245 fn issue8156(x: u8) -> u64 {
246     match x {
247         80 => {
248             return 10;
249         },
250         _ => {
251             return 100;
252         },
253     };
254 }
255
256 // Ideally the compiler should throw `unused_braces` in this case
257 fn issue9192() -> i32 {
258     {
259         return 0;
260     };
261 }
262
263 fn issue9503(x: usize) -> isize {
264     unsafe {
265         if x > 12 {
266             return *(x as *const isize);
267         } else {
268             return !*(x as *const isize);
269         };
270     };
271 }
272
273 mod issue9416 {
274     pub fn with_newline() {
275         let _ = 42;
276
277         return;
278     }
279
280     #[rustfmt::skip]
281     pub fn oneline() {
282         let _ = 42; return;
283     }
284 }
285
286 fn issue9947() -> Result<(), String> {
287     do yeet "hello";
288 }
289
290 fn main() {}