Auto merge of #13214 - apoisternex:issue12907, r=Centri3

Fix [`needless_return`] false negative

Fixes #12907

changelog: Fix [`needless_return`] false negative when returned expression borrows a value.
This commit is contained in:
bors 2024-09-07 21:47:32 +00:00
commit 41dc86d4d3
5 changed files with 41 additions and 7 deletions

View File

@ -1,7 +1,7 @@
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then}; use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
use clippy_utils::source::{snippet_with_context, SpanRangeExt}; use clippy_utils::source::{snippet_with_context, SpanRangeExt};
use clippy_utils::sugg::has_enclosing_paren; use clippy_utils::sugg::has_enclosing_paren;
use clippy_utils::visitors::{for_each_expr, Descend}; use clippy_utils::visitors::{for_each_expr, for_each_unconsumed_temporary, Descend};
use clippy_utils::{ use clippy_utils::{
binary_expr_needs_parentheses, fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor, path_res, binary_expr_needs_parentheses, fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor, path_res,
path_to_local_id, span_contains_cfg, span_find_starting_semi, path_to_local_id, span_contains_cfg, span_find_starting_semi,
@ -389,10 +389,24 @@ fn check_final_expr<'tcx>(
} }
}; };
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner)); if let Some(inner) = inner {
if borrows { if for_each_unconsumed_temporary(cx, inner, |temporary_ty| {
if temporary_ty.has_significant_drop(cx.tcx, cx.param_env)
&& temporary_ty
.walk()
.any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(re) if !re.is_static()))
{
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}
})
.is_break()
{
return; return;
} }
}
if ret_span.from_expansion() { if ret_span.from_expansion() {
return; return;
} }

View File

@ -217,12 +217,12 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
}, },
ExprKind::MethodCall(.., args, _) => { ExprKind::MethodCall(.., args, _) => {
cx.typeck_results().type_dependent_def_id(parent.hir_id).map(|did| { cx.typeck_results().type_dependent_def_id(parent.hir_id).map(|did| {
return ( (
did, did,
args, args,
cx.typeck_results().node_args(parent.hir_id), cx.typeck_results().node_args(parent.hir_id),
MethodOrFunction::Method, MethodOrFunction::Method,
); )
}) })
}, },
_ => None, _ => None,

View File

@ -355,4 +355,8 @@ fn conjunctive_blocks() -> String {
({ "a".to_string() } + "b" + { "c" }) ({ "a".to_string() } + "b" + { "c" })
} }
fn issue12907() -> String {
"".split("").next().unwrap().to_string()
}
fn main() {} fn main() {}

View File

@ -365,4 +365,8 @@ fn conjunctive_blocks() -> String {
return { "a".to_string() } + "b" + { "c" }; return { "a".to_string() } + "b" + { "c" };
} }
fn issue12907() -> String {
return "".split("").next().unwrap().to_string();
}
fn main() {} fn main() {}

View File

@ -665,5 +665,17 @@ LL - return { "a".to_string() } + "b" + { "c" };
LL + ({ "a".to_string() } + "b" + { "c" }) LL + ({ "a".to_string() } + "b" + { "c" })
| |
error: aborting due to 53 previous errors error: unneeded `return` statement
--> tests/ui/needless_return.rs:369:5
|
LL | return "".split("").next().unwrap().to_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove `return`
|
LL - return "".split("").next().unwrap().to_string();
LL + "".split("").next().unwrap().to_string()
|
error: aborting due to 54 previous errors