Allow pass by ref when returning ADT with ref

This is a follow-up to #2951 that extends the logic to allow for
returning references inside structs/enums/unions. This was a simple
oversight in the first version and it's surprisingly easy to handle.
This commit is contained in:
Ryan Cumming 2018-08-02 17:57:49 +10:00
parent c27cdcaf71
commit 08d6b3d2f6
3 changed files with 48 additions and 31 deletions

View file

@ -124,10 +124,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
// Use lifetimes to determine if we're returning a reference to the
// argument. In that case we can't switch to pass-by-value as the
// argument will not live long enough.
let output_lt = if let TypeVariants::TyRef(output_lt, _, _) = fn_sig.output().sty {
Some(output_lt)
} else {
None
let output_lts = match fn_sig.output().sty {
TypeVariants::TyRef(output_lt, _, _) => vec![output_lt],
TypeVariants::TyAdt(_, substs) => substs.regions().collect(),
_ => vec![],
};
for ((input, &ty), arg) in decl.inputs.iter().zip(fn_sig.inputs()).zip(&body.arguments) {
@ -138,7 +138,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
if_chain! {
if let TypeVariants::TyRef(input_lt, ty, Mutability::MutImmutable) = ty.sty;
if Some(input_lt) != output_lt;
if !output_lts.contains(&input_lt);
if is_copy(cx, ty);
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
if size <= self.limit;