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