]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_return.fixed
Auto merge of #8359 - flip1995:rustup, r=flip1995
[rust.git] / tests / ui / needless_return.fixed
1 // run-rustfix
2
3 #![feature(let_else)]
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 macro_rules! the_answer {
14     () => {
15         42
16     };
17 }
18
19 fn test_end_of_fn() -> bool {
20     if true {
21         // no error!
22         return true;
23     }
24     true
25 }
26
27 fn test_no_semicolon() -> bool {
28     true
29 }
30
31 fn test_if_block() -> bool {
32     if true {
33         true
34     } else {
35         false
36     }
37 }
38
39 fn test_match(x: bool) -> bool {
40     match x {
41         true => false,
42         false => {
43             true
44         },
45     }
46 }
47
48 fn test_closure() {
49     let _ = || {
50         true
51     };
52     let _ = || true;
53 }
54
55 fn test_macro_call() -> i32 {
56     return the_answer!();
57 }
58
59 fn test_void_fun() {
60     
61 }
62
63 fn test_void_if_fun(b: bool) {
64     if b {
65         
66     } else {
67         
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
89 fn read_line() -> String {
90     use std::io::BufRead;
91     let stdin = ::std::io::stdin();
92     return stdin.lock().lines().next().unwrap().unwrap();
93 }
94
95 fn borrows_but_not_last(value: bool) -> String {
96     if value {
97         use std::io::BufRead;
98         let stdin = ::std::io::stdin();
99         let _a = stdin.lock().lines().next().unwrap().unwrap();
100         String::from("test")
101     } else {
102         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(|_| {})
125     }
126
127     fn test_closure() {
128         let _ = || {
129             
130         };
131         let _ = || {};
132     }
133
134     struct Foo;
135     #[allow(clippy::unnecessary_lazy_evaluations)]
136     fn bar(res: Result<Foo, u8>) -> Foo {
137         res.unwrap_or_else(|_| Foo)
138     }
139 }
140
141 async fn async_test_end_of_fn() -> bool {
142     if true {
143         // no error!
144         return true;
145     }
146     true
147 }
148
149 async fn async_test_no_semicolon() -> bool {
150     true
151 }
152
153 async fn async_test_if_block() -> bool {
154     if true {
155         true
156     } else {
157         false
158     }
159 }
160
161 async fn async_test_match(x: bool) -> bool {
162     match x {
163         true => false,
164         false => {
165             true
166         },
167     }
168 }
169
170 async fn async_test_closure() {
171     let _ = || {
172         true
173     };
174     let _ = || 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     
183 }
184
185 async fn async_test_void_if_fun(b: bool) {
186     if b {
187         
188     } else {
189         
190     }
191 }
192
193 async fn async_test_void_match(x: u32) {
194     match x {
195         0 => (),
196         _ => (),
197     }
198 }
199
200 async fn async_read_line() -> String {
201     use std::io::BufRead;
202     let stdin = ::std::io::stdin();
203     return stdin.lock().lines().next().unwrap().unwrap();
204 }
205
206 async fn async_borrows_but_not_last(value: bool) -> String {
207     if value {
208         use std::io::BufRead;
209         let stdin = ::std::io::stdin();
210         let _a = stdin.lock().lines().next().unwrap().unwrap();
211         String::from("test")
212     } else {
213         String::new()
214     }
215 }
216
217 async fn async_test_return_in_macro() {
218     needed_return!(10);
219     needed_return!(0);
220 }
221
222 fn let_else() {
223     let Some(1) = Some(1) else { return };
224 }
225
226 fn main() {}