]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_return.rs
Improve heuristics for determining whether eager of lazy evaluation is preferred
[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 read_line() -> String {
79     use std::io::BufRead;
80     let stdin = ::std::io::stdin();
81     return stdin.lock().lines().next().unwrap().unwrap();
82 }
83
84 fn borrows_but_not_last(value: bool) -> String {
85     if value {
86         use std::io::BufRead;
87         let stdin = ::std::io::stdin();
88         let _a = stdin.lock().lines().next().unwrap().unwrap();
89         return String::from("test");
90     } else {
91         return String::new();
92     }
93 }
94
95 macro_rules! needed_return {
96     ($e:expr) => {
97         if $e > 3 {
98             return;
99         }
100     };
101 }
102
103 fn test_return_in_macro() {
104     // This will return and the macro below won't be executed. Removing the `return` from the macro
105     // will change semantics.
106     needed_return!(10);
107     needed_return!(0);
108 }
109
110 mod issue6501 {
111     #[allow(clippy::unnecessary_lazy_evaluations)]
112     fn foo(bar: Result<(), ()>) {
113         bar.unwrap_or_else(|_| return)
114     }
115
116     fn test_closure() {
117         let _ = || {
118             return;
119         };
120         let _ = || return;
121     }
122
123     struct Foo;
124     #[allow(clippy::unnecessary_lazy_evaluations)]
125     fn bar(res: Result<Foo, u8>) -> Foo {
126         res.unwrap_or_else(|_| return Foo)
127     }
128 }
129
130 async fn async_test_end_of_fn() -> bool {
131     if true {
132         // no error!
133         return true;
134     }
135     return true;
136 }
137
138 async fn async_test_no_semicolon() -> bool {
139     return true;
140 }
141
142 async fn async_test_if_block() -> bool {
143     if true {
144         return true;
145     } else {
146         return false;
147     }
148 }
149
150 async fn async_test_match(x: bool) -> bool {
151     match x {
152         true => return false,
153         false => {
154             return true;
155         },
156     }
157 }
158
159 async fn async_test_closure() {
160     let _ = || {
161         return true;
162     };
163     let _ = || return 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     return;
172 }
173
174 async fn async_test_void_if_fun(b: bool) {
175     if b {
176         return;
177     } else {
178         return;
179     }
180 }
181
182 async fn async_test_void_match(x: u32) {
183     match x {
184         0 => (),
185         _ => return,
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         return String::from("test");
201     } else {
202         return 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() {}