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 // 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. In that case we can't switch to pass-by-value as the
// argument will not live long enough. // argument will not live long enough.
let output_lt = if let TypeVariants::TyRef(output_lt, _, _) = fn_sig.output().sty { let output_lts = match fn_sig.output().sty {
Some(output_lt) TypeVariants::TyRef(output_lt, _, _) => vec![output_lt],
} else { TypeVariants::TyAdt(_, substs) => substs.regions().collect(),
None _ => vec![],
}; };
for ((input, &ty), arg) in decl.inputs.iter().zip(fn_sig.inputs()).zip(&body.arguments) { 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_chain! {
if let TypeVariants::TyRef(input_lt, ty, Mutability::MutImmutable) = ty.sty; 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 is_copy(cx, ty);
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes()); if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
if size <= self.limit; if size <= self.limit;

View File

@ -6,6 +6,10 @@ struct Foo(u32);
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
struct Bar([u8; 24]); struct Bar([u8; 24]);
struct FooRef<'a> {
foo: &'a Foo,
}
type Baz = u32; type Baz = u32;
fn good(a: &mut u32, b: u32, c: &Bar) { fn good(a: &mut u32, b: u32, c: &Bar) {
@ -20,6 +24,19 @@ fn good_return_explicit_lt_ref<'a>(foo: &'a Foo) -> &'a u32 {
&foo.0 &foo.0
} }
fn good_return_implicit_lt_struct(foo: &Foo) -> FooRef {
FooRef {
foo,
}
}
#[allow(needless_lifetimes)]
fn good_return_explicit_lt_struct<'a>(foo: &'a Foo) -> FooRef<'a> {
FooRef {
foo,
}
}
fn bad(x: &u32, y: &Foo, z: &Baz) { fn bad(x: &u32, y: &Foo, z: &Baz) {
} }

View File

@ -1,81 +1,81 @@
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:23:11 --> $DIR/trivially_copy_pass_by_ref.rs:40:11
| |
23 | fn bad(x: &u32, y: &Foo, z: &Baz) { 40 | fn bad(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `u32` | ^^^^ help: consider passing by value instead: `u32`
| |
= note: `-D trivially-copy-pass-by-ref` implied by `-D warnings` = note: `-D trivially-copy-pass-by-ref` implied by `-D warnings`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:23:20 --> $DIR/trivially_copy_pass_by_ref.rs:40:20
| |
23 | fn bad(x: &u32, y: &Foo, z: &Baz) { 40 | fn bad(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Foo` | ^^^^ help: consider passing by value instead: `Foo`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:23:29 --> $DIR/trivially_copy_pass_by_ref.rs:40:29
| |
23 | fn bad(x: &u32, y: &Foo, z: &Baz) { 40 | fn bad(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Baz` | ^^^^ help: consider passing by value instead: `Baz`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:33:12 --> $DIR/trivially_copy_pass_by_ref.rs:50:12
| |
33 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) { 50 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
| ^^^^^ help: consider passing by value instead: `self` | ^^^^^ help: consider passing by value instead: `self`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:33:22 --> $DIR/trivially_copy_pass_by_ref.rs:50:22
| |
33 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) { 50 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `u32` | ^^^^ help: consider passing by value instead: `u32`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:33:31 --> $DIR/trivially_copy_pass_by_ref.rs:50:31
| |
33 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) { 50 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Foo` | ^^^^ help: consider passing by value instead: `Foo`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:33:40 --> $DIR/trivially_copy_pass_by_ref.rs:50:40
| |
33 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) { 50 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Baz` | ^^^^ help: consider passing by value instead: `Baz`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:36:16 --> $DIR/trivially_copy_pass_by_ref.rs:53:16
| |
36 | fn bad2(x: &u32, y: &Foo, z: &Baz) { 53 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `u32` | ^^^^ help: consider passing by value instead: `u32`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:36:25 --> $DIR/trivially_copy_pass_by_ref.rs:53:25
| |
36 | fn bad2(x: &u32, y: &Foo, z: &Baz) { 53 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Foo` | ^^^^ help: consider passing by value instead: `Foo`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:36:34 --> $DIR/trivially_copy_pass_by_ref.rs:53:34
| |
36 | fn bad2(x: &u32, y: &Foo, z: &Baz) { 53 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Baz` | ^^^^ help: consider passing by value instead: `Baz`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:50:16 --> $DIR/trivially_copy_pass_by_ref.rs:67:16
| |
50 | fn bad2(x: &u32, y: &Foo, z: &Baz) { 67 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `u32` | ^^^^ help: consider passing by value instead: `u32`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:50:25 --> $DIR/trivially_copy_pass_by_ref.rs:67:25
| |
50 | fn bad2(x: &u32, y: &Foo, z: &Baz) { 67 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Foo` | ^^^^ help: consider passing by value instead: `Foo`
error: this argument is passed by reference, but would be more efficient if passed by value error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:50:34 --> $DIR/trivially_copy_pass_by_ref.rs:67:34
| |
50 | fn bad2(x: &u32, y: &Foo, z: &Baz) { 67 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Baz` | ^^^^ help: consider passing by value instead: `Baz`
error: aborting due to 13 previous errors error: aborting due to 13 previous errors