]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_return.rs
removing unsafe from test fn's && renaming shrink to sugg_span
[rust.git] / tests / ui / needless_return.rs
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     return true;
25 }
26
27 fn test_no_semicolon() -> bool {
28     return true;
29 }
30
31 fn test_if_block() -> bool {
32     if true {
33         return true;
34     } else {
35         return false;
36     }
37 }
38
39 fn test_match(x: bool) -> bool {
40     match x {
41         true => return false,
42         false => {
43             return true;
44         },
45     }
46 }
47
48 fn test_closure() {
49     let _ = || {
50         return true;
51     };
52     let _ = || return true;
53 }
54
55 fn test_macro_call() -> i32 {
56     return the_answer!();
57 }
58
59 fn test_void_fun() {
60     return;
61 }
62
63 fn test_void_if_fun(b: bool) {
64     if b {
65         return;
66     } else {
67         return;
68     }
69 }
70
71 fn test_void_match(x: u32) {
72     match x {
73         0 => (),
74         _ => return,
75     }
76 }
77
78 fn test_nested_match(x: u32) {
79     match x {
80         0 => (),
81         1 => {
82             let _ = 42;
83             return;
84         },
85         _ => return,
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         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_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         return String::from("test");
212     } else {
213         return 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() {}