Skip to content

Commit 41dc86d

Browse files
committed
Auto merge of rust-lang#13214 - apoisternex:issue12907, r=Centri3
Fix [`needless_return`] false negative Fixes rust-lang#12907 changelog: Fix [`needless_return`] false negative when returned expression borrows a value.
2 parents d9c4523 + 66283bf commit 41dc86d

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

clippy_lints/src/returns.rs

+18-4
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_with_context, SpanRangeExt};
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,
@@ -389,10 +389,24 @@ fn check_final_expr<'tcx>(
389389
}
390390
};
391391

392-
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
393-
if borrows {
394-
return;
392+
if let Some(inner) = inner {
393+
if for_each_unconsumed_temporary(cx, inner, |temporary_ty| {
394+
if temporary_ty.has_significant_drop(cx.tcx, cx.param_env)
395+
&& temporary_ty
396+
.walk()
397+
.any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(re) if !re.is_static()))
398+
{
399+
ControlFlow::Break(())
400+
} else {
401+
ControlFlow::Continue(())
402+
}
403+
})
404+
.is_break()
405+
{
406+
return;
407+
}
395408
}
409+
396410
if ret_span.from_expansion() {
397411
return;
398412
}

clippy_lints/src/useless_conversion.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
217217
},
218218
ExprKind::MethodCall(.., args, _) => {
219219
cx.typeck_results().type_dependent_def_id(parent.hir_id).map(|did| {
220-
return (
220+
(
221221
did,
222222
args,
223223
cx.typeck_results().node_args(parent.hir_id),
224224
MethodOrFunction::Method,
225-
);
225+
)
226226
})
227227
},
228228
_ => None,

tests/ui/needless_return.fixed

+4
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

+4
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

+13-1
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)