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