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