From 83f1abff4874a66124a6aaefec248ca4051c0d23 Mon Sep 17 00:00:00 2001 From: ThibsG Date: Mon, 4 Jan 2021 21:20:44 +0100 Subject: [PATCH 1/2] Fix FP with empty return for `needless_return` lint --- clippy_lints/src/returns.rs | 11 ++++++++++- tests/ui/needless_return.fixed | 13 +++++++++++++ tests/ui/needless_return.rs | 13 +++++++++++++ tests/ui/needless_return.stderr | 20 +++++++++++++++++++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 63548d8fdb43..e438f92b136a 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -131,7 +131,16 @@ impl<'tcx> LateLintPass<'tcx> for Return { _: HirId, ) { match kind { - FnKind::Closure(_) => check_final_expr(cx, &body.value, Some(body.value.span), RetReplacement::Empty), + FnKind::Closure(_) => { + // when returning without value in closure, replace this `return` + // with an empty block to prevent invalid suggestion (see #6501) + let replacement = if let ExprKind::Ret(None) = &body.value.kind { + RetReplacement::Block + } else { + RetReplacement::Empty + }; + check_final_expr(cx, &body.value, Some(body.value.span), replacement) + }, FnKind::ItemFn(..) | FnKind::Method(..) => { if let ExprKind::Block(ref block, _) = body.value.kind { check_block_return(cx, block); diff --git a/tests/ui/needless_return.fixed b/tests/ui/needless_return.fixed index 86bfc5b4bb28..f137e8ecae93 100644 --- a/tests/ui/needless_return.fixed +++ b/tests/ui/needless_return.fixed @@ -101,6 +101,19 @@ fn test_return_in_macro() { needed_return!(0); } +mod issue6501 { + fn foo(bar: Result<(), ()>) { + bar.unwrap_or_else(|_| {}) + } + + fn test_closure() { + let _ = || { + + }; + let _ = || {}; + } +} + fn main() { let _ = test_end_of_fn(); let _ = test_no_semicolon(); diff --git a/tests/ui/needless_return.rs b/tests/ui/needless_return.rs index 51061370dfe7..d754e4d37a84 100644 --- a/tests/ui/needless_return.rs +++ b/tests/ui/needless_return.rs @@ -101,6 +101,19 @@ fn test_return_in_macro() { needed_return!(0); } +mod issue6501 { + fn foo(bar: Result<(), ()>) { + bar.unwrap_or_else(|_| return) + } + + fn test_closure() { + let _ = || { + return; + }; + let _ = || return; + } +} + fn main() { let _ = test_end_of_fn(); let _ = test_no_semicolon(); diff --git a/tests/ui/needless_return.stderr b/tests/ui/needless_return.stderr index f73c833a801f..12d94e892eda 100644 --- a/tests/ui/needless_return.stderr +++ b/tests/ui/needless_return.stderr @@ -84,5 +84,23 @@ error: unneeded `return` statement LL | return String::new(); | ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()` -error: aborting due to 14 previous errors +error: unneeded `return` statement + --> $DIR/needless_return.rs:91:32 + | +LL | bar.unwrap_or_else(|_| return) + | ^^^^^^ help: replace `return` with an empty block: `{}` + +error: unneeded `return` statement + --> $DIR/needless_return.rs:96:13 + | +LL | return; + | ^^^^^^^ help: remove `return` + +error: unneeded `return` statement + --> $DIR/needless_return.rs:98:20 + | +LL | let _ = || return; + | ^^^^^^ help: replace `return` with an empty block: `{}` + +error: aborting due to 17 previous errors From 46aa654c2d1792a42835d63f9a3dfcbffc20b758 Mon Sep 17 00:00:00 2001 From: ThibsG Date: Fri, 15 Jan 2021 18:58:48 +0100 Subject: [PATCH 2/2] Fix test due to recent Rustup merge --- tests/ui/needless_return.stderr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ui/needless_return.stderr b/tests/ui/needless_return.stderr index 12d94e892eda..d1240e161c07 100644 --- a/tests/ui/needless_return.stderr +++ b/tests/ui/needless_return.stderr @@ -85,19 +85,19 @@ LL | return String::new(); | ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()` error: unneeded `return` statement - --> $DIR/needless_return.rs:91:32 + --> $DIR/needless_return.rs:106:32 | LL | bar.unwrap_or_else(|_| return) | ^^^^^^ help: replace `return` with an empty block: `{}` error: unneeded `return` statement - --> $DIR/needless_return.rs:96:13 + --> $DIR/needless_return.rs:111:13 | LL | return; | ^^^^^^^ help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:98:20 + --> $DIR/needless_return.rs:113:20 | LL | let _ = || return; | ^^^^^^ help: replace `return` with an empty block: `{}`