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