]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/needless_return.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / needless_return.rs
CommitLineData
f20569fa 1// run-rustfix
cdc7bbd5 2
c295e0f8 3#![feature(let_else)]
cdc7bbd5
XL
4#![allow(unused)]
5#![allow(
6 clippy::if_same_then_else,
7 clippy::single_match,
c295e0f8
XL
8 clippy::needless_bool,
9 clippy::equatable_if_let
cdc7bbd5 10)]
f20569fa
XL
11#![warn(clippy::needless_return)]
12
13macro_rules! the_answer {
14 () => {
15 42
16 };
17}
18
19fn test_end_of_fn() -> bool {
20 if true {
21 // no error!
22 return true;
23 }
24 return true;
25}
26
27fn test_no_semicolon() -> bool {
28 return true;
29}
30
31fn test_if_block() -> bool {
32 if true {
33 return true;
34 } else {
35 return false;
36 }
37}
38
39fn test_match(x: bool) -> bool {
40 match x {
41 true => return false,
42 false => {
43 return true;
44 },
45 }
46}
47
48fn test_closure() {
49 let _ = || {
50 return true;
51 };
52 let _ = || return true;
53}
54
55fn test_macro_call() -> i32 {
56 return the_answer!();
57}
58
59fn test_void_fun() {
60 return;
61}
62
63fn test_void_if_fun(b: bool) {
64 if b {
65 return;
66 } else {
67 return;
68 }
69}
70
71fn test_void_match(x: u32) {
72 match x {
73 0 => (),
74 _ => return,
75 }
76}
77
a2a8927a
XL
78fn test_nested_match(x: u32) {
79 match x {
80 0 => (),
81 1 => {
82 let _ = 42;
83 return;
84 },
85 _ => return,
86 }
87}
88
f20569fa
XL
89fn read_line() -> String {
90 use std::io::BufRead;
91 let stdin = ::std::io::stdin();
92 return stdin.lock().lines().next().unwrap().unwrap();
93}
94
95fn 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
106macro_rules! needed_return {
107 ($e:expr) => {
108 if $e > 3 {
109 return;
110 }
111 };
112}
113
114fn 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
121mod issue6501 {
a2a8927a 122 #[allow(clippy::unnecessary_lazy_evaluations)]
f20569fa
XL
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
cdc7bbd5
XL
141async fn async_test_end_of_fn() -> bool {
142 if true {
143 // no error!
144 return true;
145 }
146 return true;
147}
148
149async fn async_test_no_semicolon() -> bool {
150 return true;
151}
152
153async fn async_test_if_block() -> bool {
154 if true {
155 return true;
156 } else {
157 return false;
158 }
f20569fa 159}
cdc7bbd5
XL
160
161async fn async_test_match(x: bool) -> bool {
162 match x {
163 true => return false,
164 false => {
165 return true;
166 },
167 }
168}
169
170async fn async_test_closure() {
171 let _ = || {
172 return true;
173 };
174 let _ = || return true;
175}
176
177async fn async_test_macro_call() -> i32 {
178 return the_answer!();
179}
180
181async fn async_test_void_fun() {
182 return;
183}
184
185async fn async_test_void_if_fun(b: bool) {
186 if b {
187 return;
188 } else {
189 return;
190 }
191}
192
193async fn async_test_void_match(x: u32) {
194 match x {
195 0 => (),
196 _ => return,
197 }
198}
199
200async 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
206async 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
217async fn async_test_return_in_macro() {
218 needed_return!(10);
219 needed_return!(0);
220}
221
c295e0f8
XL
222fn let_else() {
223 let Some(1) = Some(1) else { return };
224}
225
923072b8
FG
226fn needless_return_macro() -> String {
227 let _ = "foo";
228 let _ = "bar";
229 return format!("Hello {}", "world!");
230}
231
cdc7bbd5 232fn main() {}