From 19999c1a01ed74c999bb86affeb48f74475e1c8b Mon Sep 17 00:00:00 2001 From: dswij Date: Thu, 16 Jun 2022 23:14:18 +0800 Subject: [PATCH] emit `needless_return` on borrowed return --- clippy_lints/src/returns.rs | 15 ++++++--------- tests/ui/needless_return.fixed | 4 ++-- tests/ui/needless_return.stderr | 14 +++++++++++++- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index e525eba53e2a..b94acbaf7778 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -181,15 +181,12 @@ fn check_final_expr<'tcx>( // allow `#[cfg(a)] return a; #[cfg(b)] return b;` let attrs = cx.tcx.hir().attrs(expr.hir_id); if !attrs.iter().any(attr_is_cfg) { - let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner)); - if !borrows { - emit_return_lint( - cx, - span.expect("`else return` is not possible"), - inner.as_ref().map(|i| i.span), - replacement, - ); - } + emit_return_lint( + cx, + span.expect("`else return` is not possible"), + inner.as_ref().map(|i| i.span), + replacement, + ); } }, // a whole block? check it! diff --git a/tests/ui/needless_return.fixed b/tests/ui/needless_return.fixed index 7c828430b785..1d886c89afef 100644 --- a/tests/ui/needless_return.fixed +++ b/tests/ui/needless_return.fixed @@ -89,7 +89,7 @@ fn test_nested_match(x: u32) { fn read_line() -> String { use std::io::BufRead; let stdin = ::std::io::stdin(); - return stdin.lock().lines().next().unwrap().unwrap(); + stdin.lock().lines().next().unwrap().unwrap() } fn borrows_but_not_last(value: bool) -> String { @@ -200,7 +200,7 @@ async fn async_test_void_match(x: u32) { async fn async_read_line() -> String { use std::io::BufRead; let stdin = ::std::io::stdin(); - return stdin.lock().lines().next().unwrap().unwrap(); + stdin.lock().lines().next().unwrap().unwrap() } async fn async_borrows_but_not_last(value: bool) -> String { diff --git a/tests/ui/needless_return.stderr b/tests/ui/needless_return.stderr index 4c8be47b025e..d34caaf75017 100644 --- a/tests/ui/needless_return.stderr +++ b/tests/ui/needless_return.stderr @@ -90,6 +90,12 @@ error: unneeded `return` statement LL | _ => return, | ^^^^^^ help: replace `return` with a unit value: `()` +error: unneeded `return` statement + --> $DIR/needless_return.rs:92:5 + | +LL | return stdin.lock().lines().next().unwrap().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `stdin.lock().lines().next().unwrap().unwrap()` + error: unneeded `return` statement --> $DIR/needless_return.rs:100:9 | @@ -204,6 +210,12 @@ error: unneeded `return` statement LL | _ => return, | ^^^^^^ help: replace `return` with a unit value: `()` +error: unneeded `return` statement + --> $DIR/needless_return.rs:203:5 + | +LL | return stdin.lock().lines().next().unwrap().unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `stdin.lock().lines().next().unwrap().unwrap()` + error: unneeded `return` statement --> $DIR/needless_return.rs:211:9 | @@ -222,5 +234,5 @@ error: unneeded `return` statement LL | return format!("Hello {}", "world!"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `format!("Hello {}", "world!")` -error: aborting due to 37 previous errors +error: aborting due to 39 previous errors