]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_return.fixed
Rollup merge of #101634 - aDotInTheVoid:rdj-test, r=CraftSpider
[rust.git] / src / tools / clippy / tests / ui / needless_return.fixed
1 // run-rustfix
2
3 #![feature(lint_reasons)]
4 #![feature(let_else)]
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
66 fn test_void_if_fun(b: bool) {
67     if b {
68         
69     } else {
70         
71     }
72 }
73
74 fn test_void_match(x: u32) {
75     match x {
76         0 => (),
77         _ => (),
78     }
79 }
80
81 fn test_nested_match(x: u32) {
82     match x {
83         0 => (),
84         1 => {
85             let _ = 42;
86             
87         },
88         _ => (),
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         String::from("test")
102     } else {
103         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(|_| {})
126     }
127
128     fn test_closure() {
129         let _ = || {
130             
131         };
132         let _ = || {};
133     }
134
135     struct Foo;
136     #[allow(clippy::unnecessary_lazy_evaluations)]
137     fn bar(res: Result<Foo, u8>) -> Foo {
138         res.unwrap_or_else(|_| Foo)
139     }
140 }
141
142 async fn async_test_end_of_fn() -> bool {
143     if true {
144         // no error!
145         return true;
146     }
147     true
148 }
149
150 async fn async_test_no_semicolon() -> bool {
151     true
152 }
153
154 async fn async_test_if_block() -> bool {
155     if true {
156         true
157     } else {
158         false
159     }
160 }
161
162 async fn async_test_match(x: bool) -> bool {
163     match x {
164         true => false,
165         false => {
166             true
167         },
168     }
169 }
170
171 async fn async_test_closure() {
172     let _ = || {
173         true
174     };
175     let _ = || true;
176 }
177
178 async fn async_test_macro_call() -> i32 {
179     the_answer!()
180 }
181
182 async fn async_test_void_fun() {
183     
184 }
185
186 async fn async_test_void_if_fun(b: bool) {
187     if b {
188         
189     } else {
190         
191     }
192 }
193
194 async fn async_test_void_match(x: u32) {
195     match x {
196         0 => (),
197         _ => (),
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         String::from("test")
211     } else {
212         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     format!("Hello {}", "world!")
229 }
230
231 fn issue_9361() -> i32 {
232     #[allow(clippy::integer_arithmetic)]
233     return 1 + 2;
234 }
235
236 fn main() {}