Auto merge of #7067 - TaKO8Ki:fix-false-negative-on-needless-return, r=llogiq

Fix a false negative on `needless return`

closes #7042

changelog: fix a false negative on `needless return`
This commit is contained in:
bors 2021-04-12 18:00:38 +00:00
commit 2a96bc434b
4 changed files with 268 additions and 31 deletions

View File

@ -223,6 +223,7 @@ fn check_final_expr<'tcx>(
},
_ => (),
},
ExprKind::DropTemps(expr) => check_final_expr(cx, expr, None, RetReplacement::Empty),
_ => (),
}
}

View File

@ -1,4 +1,5 @@
// run-rustfix
// edition:2018
#![allow(unused)]
#![allow(
@ -125,10 +126,85 @@ mod issue6501 {
}
}
fn main() {
let _ = test_end_of_fn();
let _ = test_no_semicolon();
let _ = test_if_block();
let _ = test_match(true);
test_closure();
async fn async_test_end_of_fn() -> bool {
if true {
// no error!
return true;
}
true
}
async fn async_test_no_semicolon() -> bool {
true
}
async fn async_test_if_block() -> bool {
if true {
true
} else {
false
}
}
async fn async_test_match(x: bool) -> bool {
match x {
true => false,
false => {
true
},
}
}
async fn async_test_closure() {
let _ = || {
true
};
let _ = || true;
}
async fn async_test_macro_call() -> i32 {
return the_answer!();
}
async fn async_test_void_fun() {
}
async fn async_test_void_if_fun(b: bool) {
if b {
} else {
}
}
async fn async_test_void_match(x: u32) {
match x {
0 => (),
_ => {},
}
}
async fn async_read_line() -> String {
use std::io::BufRead;
let stdin = ::std::io::stdin();
return stdin.lock().lines().next().unwrap().unwrap();
}
async fn async_borrows_but_not_last(value: bool) -> String {
if value {
use std::io::BufRead;
let stdin = ::std::io::stdin();
let _a = stdin.lock().lines().next().unwrap().unwrap();
String::from("test")
} else {
String::new()
}
}
async fn async_test_return_in_macro() {
needed_return!(10);
needed_return!(0);
}
fn main() {}

View File

@ -1,4 +1,5 @@
// run-rustfix
// edition:2018
#![allow(unused)]
#![allow(
@ -125,10 +126,85 @@ fn bar(res: Result<Foo, u8>) -> Foo {
}
}
fn main() {
let _ = test_end_of_fn();
let _ = test_no_semicolon();
let _ = test_if_block();
let _ = test_match(true);
test_closure();
async fn async_test_end_of_fn() -> bool {
if true {
// no error!
return true;
}
return true;
}
async fn async_test_no_semicolon() -> bool {
return true;
}
async fn async_test_if_block() -> bool {
if true {
return true;
} else {
return false;
}
}
async fn async_test_match(x: bool) -> bool {
match x {
true => return false,
false => {
return true;
},
}
}
async fn async_test_closure() {
let _ = || {
return true;
};
let _ = || return true;
}
async fn async_test_macro_call() -> i32 {
return the_answer!();
}
async fn async_test_void_fun() {
return;
}
async fn async_test_void_if_fun(b: bool) {
if b {
return;
} else {
return;
}
}
async fn async_test_void_match(x: u32) {
match x {
0 => (),
_ => return,
}
}
async fn async_read_line() -> String {
use std::io::BufRead;
let stdin = ::std::io::stdin();
return stdin.lock().lines().next().unwrap().unwrap();
}
async fn async_borrows_but_not_last(value: bool) -> String {
if value {
use std::io::BufRead;
let stdin = ::std::io::stdin();
let _a = stdin.lock().lines().next().unwrap().unwrap();
return String::from("test");
} else {
return String::new();
}
}
async fn async_test_return_in_macro() {
needed_return!(10);
needed_return!(0);
}
fn main() {}

View File

@ -1,5 +1,5 @@
error: unneeded `return` statement
--> $DIR/needless_return.rs:23:5
--> $DIR/needless_return.rs:24:5
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
@ -7,106 +7,190 @@ LL | return true;
= note: `-D clippy::needless-return` implied by `-D warnings`
error: unneeded `return` statement
--> $DIR/needless_return.rs:27:5
--> $DIR/needless_return.rs:28:5
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:32:9
--> $DIR/needless_return.rs:33:9
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:34:9
--> $DIR/needless_return.rs:35:9
|
LL | return false;
| ^^^^^^^^^^^^^ help: remove `return`: `false`
error: unneeded `return` statement
--> $DIR/needless_return.rs:40:17
--> $DIR/needless_return.rs:41:17
|
LL | true => return false,
| ^^^^^^^^^^^^ help: remove `return`: `false`
error: unneeded `return` statement
--> $DIR/needless_return.rs:42:13
--> $DIR/needless_return.rs:43:13
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:49:9
--> $DIR/needless_return.rs:50:9
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:51:16
--> $DIR/needless_return.rs:52:16
|
LL | let _ = || return true;
| ^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:59:5
--> $DIR/needless_return.rs:60:5
|
LL | return;
| ^^^^^^^ help: remove `return`
error: unneeded `return` statement
--> $DIR/needless_return.rs:64:9
--> $DIR/needless_return.rs:65:9
|
LL | return;
| ^^^^^^^ help: remove `return`
error: unneeded `return` statement
--> $DIR/needless_return.rs:66:9
--> $DIR/needless_return.rs:67:9
|
LL | return;
| ^^^^^^^ help: remove `return`
error: unneeded `return` statement
--> $DIR/needless_return.rs:73:14
--> $DIR/needless_return.rs:74:14
|
LL | _ => return,
| ^^^^^^ help: replace `return` with an empty block: `{}`
error: unneeded `return` statement
--> $DIR/needless_return.rs:88:9
--> $DIR/needless_return.rs:89:9
|
LL | return String::from("test");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::from("test")`
error: unneeded `return` statement
--> $DIR/needless_return.rs:90:9
--> $DIR/needless_return.rs:91:9
|
LL | return String::new();
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`
error: unneeded `return` statement
--> $DIR/needless_return.rs:111:32
--> $DIR/needless_return.rs:112:32
|
LL | bar.unwrap_or_else(|_| return)
| ^^^^^^ help: replace `return` with an empty block: `{}`
error: unneeded `return` statement
--> $DIR/needless_return.rs:116:13
--> $DIR/needless_return.rs:117:13
|
LL | return;
| ^^^^^^^ help: remove `return`
error: unneeded `return` statement
--> $DIR/needless_return.rs:118:20
--> $DIR/needless_return.rs:119:20
|
LL | let _ = || return;
| ^^^^^^ help: replace `return` with an empty block: `{}`
error: unneeded `return` statement
--> $DIR/needless_return.rs:124:32
--> $DIR/needless_return.rs:125:32
|
LL | res.unwrap_or_else(|_| return Foo)
| ^^^^^^^^^^ help: remove `return`: `Foo`
error: aborting due to 18 previous errors
error: unneeded `return` statement
--> $DIR/needless_return.rs:134:5
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:138:5
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:143:9
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:145:9
|
LL | return false;
| ^^^^^^^^^^^^^ help: remove `return`: `false`
error: unneeded `return` statement
--> $DIR/needless_return.rs:151:17
|
LL | true => return false,
| ^^^^^^^^^^^^ help: remove `return`: `false`
error: unneeded `return` statement
--> $DIR/needless_return.rs:153:13
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:160:9
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:162:16
|
LL | let _ = || return true;
| ^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement
--> $DIR/needless_return.rs:170:5
|
LL | return;
| ^^^^^^^ help: remove `return`
error: unneeded `return` statement
--> $DIR/needless_return.rs:175:9
|
LL | return;
| ^^^^^^^ help: remove `return`
error: unneeded `return` statement
--> $DIR/needless_return.rs:177:9
|
LL | return;
| ^^^^^^^ help: remove `return`
error: unneeded `return` statement
--> $DIR/needless_return.rs:184:14
|
LL | _ => return,
| ^^^^^^ help: replace `return` with an empty block: `{}`
error: unneeded `return` statement
--> $DIR/needless_return.rs:199:9
|
LL | return String::from("test");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::from("test")`
error: unneeded `return` statement
--> $DIR/needless_return.rs:201:9
|
LL | return String::new();
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`
error: aborting due to 32 previous errors