Skip to content

Commit af35dcd

Browse files
committed
Fix [needless_return] false negative when returned expression borrows a value
Fixes #12907 changelog: Fix [`needless_return`] false negative when returned expression borrows a value
1 parent 377d72a commit af35dcd

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

clippy_lints/src/returns.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
22
use clippy_utils::source::{snippet_opt, snippet_with_context};
33
use clippy_utils::sugg::has_enclosing_paren;
4-
use clippy_utils::visitors::{for_each_expr, Descend};
4+
use clippy_utils::visitors::{for_each_expr, for_each_unconsumed_temporary, Descend};
55
use clippy_utils::{
66
binary_expr_needs_parentheses, fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor, path_res,
77
path_to_local_id, span_contains_cfg, span_find_starting_semi,
@@ -384,10 +384,24 @@ fn check_final_expr<'tcx>(
384384
}
385385
};
386386

387-
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
388-
if borrows {
389-
return;
387+
if let Some(inner) = inner {
388+
if for_each_unconsumed_temporary(cx, inner, |temporary_ty| {
389+
if temporary_ty.has_significant_drop(cx.tcx, cx.param_env)
390+
&& temporary_ty
391+
.walk()
392+
.any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(re) if !re.is_static()))
393+
{
394+
ControlFlow::Break(())
395+
} else {
396+
ControlFlow::Continue(())
397+
}
398+
})
399+
.is_break()
400+
{
401+
return;
402+
}
390403
}
404+
391405
if ret_span.from_expansion() {
392406
return;
393407
}

tests/ui/needless_return.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,8 @@ fn conjunctive_blocks() -> String {
355355
({ "a".to_string() } + "b" + { "c" })
356356
}
357357

358+
fn issue12907() -> String {
359+
"".split("").next().unwrap().to_string()
360+
}
361+
358362
fn main() {}

tests/ui/needless_return.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,8 @@ fn conjunctive_blocks() -> String {
365365
return { "a".to_string() } + "b" + { "c" };
366366
}
367367

368+
fn issue12907() -> String {
369+
return "".split("").next().unwrap().to_string();
370+
}
371+
368372
fn main() {}

tests/ui/needless_return.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,5 +665,17 @@ LL - return { "a".to_string() } + "b" + { "c" };
665665
LL + ({ "a".to_string() } + "b" + { "c" })
666666
|
667667

668-
error: aborting due to 53 previous errors
668+
error: unneeded `return` statement
669+
--> tests/ui/needless_return.rs:369:5
670+
|
671+
LL | return "".split("").next().unwrap().to_string();
672+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
673+
|
674+
help: remove `return`
675+
|
676+
LL - return "".split("").next().unwrap().to_string();
677+
LL + "".split("").next().unwrap().to_string()
678+
|
679+
680+
error: aborting due to 54 previous errors
669681

0 commit comments

Comments
 (0)