]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_return.rs
c42567b517c9115819b3446d5c5281c0da273884
[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     fn foo(bar: Result<(), ()>) {
112         bar.unwrap_or_else(|_| return)
113     }
114
115     fn test_closure() {
116         let _ = || {
117             return;
118         };
119         let _ = || return;
120     }
121
122     struct Foo;
123     #[allow(clippy::unnecessary_lazy_evaluations)]
124     fn bar(res: Result<Foo, u8>) -> Foo {
125         res.unwrap_or_else(|_| return Foo)
126     }
127 }
128
129 async fn async_test_end_of_fn() -> bool {
130     if true {
131         // no error!
132         return true;
133     }
134     return true;
135 }
136
137 async fn async_test_no_semicolon() -> bool {
138     return true;
139 }
140
141 async fn async_test_if_block() -> bool {
142     if true {
143         return true;
144     } else {
145         return false;
146     }
147 }
148
149 async fn async_test_match(x: bool) -> bool {
150     match x {
151         true => return false,
152         false => {
153             return true;
154         },
155     }
156 }
157
158 async fn async_test_closure() {
159     let _ = || {
160         return true;
161     };
162     let _ = || return true;
163 }
164
165 async fn async_test_macro_call() -> i32 {
166     return the_answer!();
167 }
168
169 async fn async_test_void_fun() {
170     return;
171 }
172
173 async fn async_test_void_if_fun(b: bool) {
174     if b {
175         return;
176     } else {
177         return;
178     }
179 }
180
181 async fn async_test_void_match(x: u32) {
182     match x {
183         0 => (),
184         _ => return,
185     }
186 }
187
188 async fn async_read_line() -> String {
189     use std::io::BufRead;
190     let stdin = ::std::io::stdin();
191     return stdin.lock().lines().next().unwrap().unwrap();
192 }
193
194 async fn async_borrows_but_not_last(value: bool) -> String {
195     if value {
196         use std::io::BufRead;
197         let stdin = ::std::io::stdin();
198         let _a = stdin.lock().lines().next().unwrap().unwrap();
199         return String::from("test");
200     } else {
201         return String::new();
202     }
203 }
204
205 async fn async_test_return_in_macro() {
206     needed_return!(10);
207     needed_return!(0);
208 }
209
210 fn let_else() {
211     let Some(1) = Some(1) else { return };
212 }
213
214 fn main() {}