]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_return.fixed
9c999e12b4cbc4c6d5a3afed29e97cffa3638623
[rust.git] / tests / ui / needless_return.fixed
1 // run-rustfix
2 // edition:2018
3
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 macro_rules! the_answer {
15     () => {
16         42
17     };
18 }
19
20 fn test_end_of_fn() -> bool {
21     if true {
22         // no error!
23         return true;
24     }
25     true
26 }
27
28 fn test_no_semicolon() -> bool {
29     true
30 }
31
32 fn test_if_block() -> bool {
33     if true {
34         true
35     } else {
36         false
37     }
38 }
39
40 fn test_match(x: bool) -> bool {
41     match x {
42         true => false,
43         false => {
44             true
45         },
46     }
47 }
48
49 fn test_closure() {
50     let _ = || {
51         true
52     };
53     let _ = || true;
54 }
55
56 fn test_macro_call() -> i32 {
57     return the_answer!();
58 }
59
60 fn test_void_fun() {
61     
62 }
63
64 fn test_void_if_fun(b: bool) {
65     if b {
66         
67     } else {
68         
69     }
70 }
71
72 fn test_void_match(x: u32) {
73     match x {
74         0 => (),
75         _ => {},
76     }
77 }
78
79 fn read_line() -> String {
80     use std::io::BufRead;
81     let stdin = ::std::io::stdin();
82     return stdin.lock().lines().next().unwrap().unwrap();
83 }
84
85 fn borrows_but_not_last(value: bool) -> String {
86     if value {
87         use std::io::BufRead;
88         let stdin = ::std::io::stdin();
89         let _a = stdin.lock().lines().next().unwrap().unwrap();
90         String::from("test")
91     } else {
92         String::new()
93     }
94 }
95
96 macro_rules! needed_return {
97     ($e:expr) => {
98         if $e > 3 {
99             return;
100         }
101     };
102 }
103
104 fn test_return_in_macro() {
105     // This will return and the macro below won't be executed. Removing the `return` from the macro
106     // will change semantics.
107     needed_return!(10);
108     needed_return!(0);
109 }
110
111 mod issue6501 {
112     fn foo(bar: Result<(), ()>) {
113         bar.unwrap_or_else(|_| {})
114     }
115
116     fn test_closure() {
117         let _ = || {
118             
119         };
120         let _ = || {};
121     }
122
123     struct Foo;
124     #[allow(clippy::unnecessary_lazy_evaluations)]
125     fn bar(res: Result<Foo, u8>) -> Foo {
126         res.unwrap_or_else(|_| Foo)
127     }
128 }
129
130 async fn async_test_end_of_fn() -> bool {
131     if true {
132         // no error!
133         return true;
134     }
135     true
136 }
137
138 async fn async_test_no_semicolon() -> bool {
139     true
140 }
141
142 async fn async_test_if_block() -> bool {
143     if true {
144         true
145     } else {
146         false
147     }
148 }
149
150 async fn async_test_match(x: bool) -> bool {
151     match x {
152         true => false,
153         false => {
154             true
155         },
156     }
157 }
158
159 async fn async_test_closure() {
160     let _ = || {
161         true
162     };
163     let _ = || true;
164 }
165
166 async fn async_test_macro_call() -> i32 {
167     return the_answer!();
168 }
169
170 async fn async_test_void_fun() {
171     
172 }
173
174 async fn async_test_void_if_fun(b: bool) {
175     if b {
176         
177     } else {
178         
179     }
180 }
181
182 async fn async_test_void_match(x: u32) {
183     match x {
184         0 => (),
185         _ => {},
186     }
187 }
188
189 async fn async_read_line() -> String {
190     use std::io::BufRead;
191     let stdin = ::std::io::stdin();
192     return stdin.lock().lines().next().unwrap().unwrap();
193 }
194
195 async fn async_borrows_but_not_last(value: bool) -> String {
196     if value {
197         use std::io::BufRead;
198         let stdin = ::std::io::stdin();
199         let _a = stdin.lock().lines().next().unwrap().unwrap();
200         String::from("test")
201     } else {
202         String::new()
203     }
204 }
205
206 async fn async_test_return_in_macro() {
207     needed_return!(10);
208     needed_return!(0);
209 }
210
211 fn let_else() {
212     let Some(1) = Some(1) else { return };
213 }
214
215 fn main() {}