Skip to content

Commit 0218a3b

Browse files
committed
add tests for a false negative on needless_return
1 parent 53260df commit 0218a3b

File tree

3 files changed

+333
-19
lines changed

3 files changed

+333
-19
lines changed

tests/ui/needless_return.fixed

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// edition:2018
23

34
#![allow(unused)]
45
#![allow(
@@ -125,6 +126,108 @@ mod issue6501 {
125126
}
126127
}
127128

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+
// This will return and the macro below won't be executed. Removing the `return` from the macro
207+
// will change semantics.
208+
needed_return!(10);
209+
needed_return!(0);
210+
}
211+
212+
mod async_issue6501 {
213+
async fn foo(bar: Result<(), ()>) {
214+
bar.unwrap_or_else(|_| {})
215+
}
216+
217+
async fn async_test_closure() {
218+
let _ = || {
219+
220+
};
221+
let _ = || {};
222+
}
223+
224+
struct Foo;
225+
#[allow(clippy::unnecessary_lazy_evaluations)]
226+
async fn bar(res: Result<Foo, u8>) -> Foo {
227+
res.unwrap_or_else(|_| Foo)
228+
}
229+
}
230+
128231
fn main() {
129232
let _ = test_end_of_fn();
130233
let _ = test_no_semicolon();

tests/ui/needless_return.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// edition:2018
23

34
#![allow(unused)]
45
#![allow(
@@ -125,6 +126,108 @@ mod issue6501 {
125126
}
126127
}
127128

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+
// This will return and the macro below won't be executed. Removing the `return` from the macro
207+
// will change semantics.
208+
needed_return!(10);
209+
needed_return!(0);
210+
}
211+
212+
mod async_issue6501 {
213+
async fn foo(bar: Result<(), ()>) {
214+
bar.unwrap_or_else(|_| return)
215+
}
216+
217+
async fn async_test_closure() {
218+
let _ = || {
219+
return;
220+
};
221+
let _ = || return;
222+
}
223+
224+
struct Foo;
225+
#[allow(clippy::unnecessary_lazy_evaluations)]
226+
async fn bar(res: Result<Foo, u8>) -> Foo {
227+
res.unwrap_or_else(|_| return Foo)
228+
}
229+
}
230+
128231
fn main() {
129232
let _ = test_end_of_fn();
130233
let _ = test_no_semicolon();

0 commit comments

Comments
 (0)