]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_return.fixed
Merge commit '98e2b9f25b6db4b2680a3d388456d9f95cb28344' into clippyup
[rust.git] / src / tools / clippy / tests / ui / needless_return.fixed
1 // run-rustfix
2 // edition:2018
3
4 #![allow(unused)]
5 #![allow(
6     clippy::if_same_then_else,
7     clippy::single_match,
8     clippy::branches_sharing_code,
9     clippy::needless_bool
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     true
25 }
26
27 fn test_no_semicolon() -> bool {
28     true
29 }
30
31 fn test_if_block() -> bool {
32     if true {
33         true
34     } else {
35         false
36     }
37 }
38
39 fn test_match(x: bool) -> bool {
40     match x {
41         true => false,
42         false => {
43             true
44         },
45     }
46 }
47
48 fn test_closure() {
49     let _ = || {
50         true
51     };
52     let _ = || true;
53 }
54
55 fn test_macro_call() -> i32 {
56     return the_answer!();
57 }
58
59 fn test_void_fun() {
60     
61 }
62
63 fn test_void_if_fun(b: bool) {
64     if b {
65         
66     } else {
67         
68     }
69 }
70
71 fn test_void_match(x: u32) {
72     match x {
73         0 => (),
74         _ => {},
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         String::from("test")
90     } else {
91         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(|_| {})
113     }
114
115     fn test_closure() {
116         let _ = || {
117             
118         };
119         let _ = || {};
120     }
121
122     struct Foo;
123     #[allow(clippy::unnecessary_lazy_evaluations)]
124     fn bar(res: Result<Foo, u8>) -> Foo {
125         res.unwrap_or_else(|_| Foo)
126     }
127 }
128
129 async fn async_test_end_of_fn() -> bool {
130     if true {
131         // no error!
132         return true;
133     }
134     true
135 }
136
137 async fn async_test_no_semicolon() -> bool {
138     true
139 }
140
141 async fn async_test_if_block() -> bool {
142     if true {
143         true
144     } else {
145         false
146     }
147 }
148
149 async fn async_test_match(x: bool) -> bool {
150     match x {
151         true => false,
152         false => {
153             true
154         },
155     }
156 }
157
158 async fn async_test_closure() {
159     let _ = || {
160         true
161     };
162     let _ = || 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     
171 }
172
173 async fn async_test_void_if_fun(b: bool) {
174     if b {
175         
176     } else {
177         
178     }
179 }
180
181 async fn async_test_void_match(x: u32) {
182     match x {
183         0 => (),
184         _ => {},
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         String::from("test")
200     } else {
201         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 main() {}