about summary refs log tree commit diff
path: root/tests/ui/variance
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-03-13 20:58:54 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-04-11 16:41:41 +0000
commita1a3abb08fca88ba1c76888c0201870a5f590b5a (patch)
treebffd124af056ed51796e7c0a4d26a7b582f09970 /tests/ui/variance
parent7f7f6792f161d8b7f383ca18a1d820d2ca811bc4 (diff)
downloadrust-a1a3abb08fca88ba1c76888c0201870a5f590b5a.tar.gz
rust-a1a3abb08fca88ba1c76888c0201870a5f590b5a.zip
When possible, suggest cloning the result of a call instead of an argument
```
error[E0505]: cannot move out of `a` because it is borrowed
  --> $DIR/variance-issue-20533.rs:28:14
   |
LL |         let a = AffineU32(1);
   |             - binding `a` declared here
LL |         let x = foo(&a);
   |                     -- borrow of `a` occurs here
LL |         drop(a);
   |              ^ move out of `a` occurs here
LL |         drop(x);
   |              - borrow later used here
   |
help: consider cloning the value if the performance cost is acceptable
   |
LL |         let x = foo(&a).clone();
   |                        ++++++++
```
Diffstat (limited to 'tests/ui/variance')
-rw-r--r--tests/ui/variance/variance-issue-20533.rs19
-rw-r--r--tests/ui/variance/variance-issue-20533.stderr43
2 files changed, 58 insertions, 4 deletions
diff --git a/tests/ui/variance/variance-issue-20533.rs b/tests/ui/variance/variance-issue-20533.rs
index a2459f8730c..4c109608906 100644
--- a/tests/ui/variance/variance-issue-20533.rs
+++ b/tests/ui/variance/variance-issue-20533.rs
@@ -19,8 +19,15 @@ fn baz<'a, T>(_x: &'a T) -> Baked<'a> {
     Baked(PhantomData)
 }
 
+fn bat(x: &AffineU32) -> &u32 {
+    &x.0
+}
+
 struct AffineU32(u32);
 
+#[derive(Clone)]
+struct ClonableAffineU32(u32);
+
 fn main() {
     {
         let a = AffineU32(1);
@@ -40,4 +47,16 @@ fn main() {
         drop(a); //~ ERROR cannot move out of `a`
         drop(x);
     }
+    {
+        let a = AffineU32(1);
+        let x = bat(&a);
+        drop(a); //~ ERROR cannot move out of `a`
+        drop(x);
+    }
+    {
+        let a = ClonableAffineU32(1);
+        let x = foo(&a);
+        drop(a); //~ ERROR cannot move out of `a`
+        drop(x);
+    }
 }
diff --git a/tests/ui/variance/variance-issue-20533.stderr b/tests/ui/variance/variance-issue-20533.stderr
index 258f67db5ce..7060144c776 100644
--- a/tests/ui/variance/variance-issue-20533.stderr
+++ b/tests/ui/variance/variance-issue-20533.stderr
@@ -1,5 +1,5 @@
 error[E0505]: cannot move out of `a` because it is borrowed
-  --> $DIR/variance-issue-20533.rs:28:14
+  --> $DIR/variance-issue-20533.rs:35:14
    |
 LL |         let a = AffineU32(1);
    |             - binding `a` declared here
@@ -11,7 +11,7 @@ LL |         drop(x);
    |              - borrow later used here
 
 error[E0505]: cannot move out of `a` because it is borrowed
-  --> $DIR/variance-issue-20533.rs:34:14
+  --> $DIR/variance-issue-20533.rs:41:14
    |
 LL |         let a = AffineU32(1);
    |             - binding `a` declared here
@@ -23,7 +23,7 @@ LL |         drop(x);
    |              - borrow later used here
 
 error[E0505]: cannot move out of `a` because it is borrowed
-  --> $DIR/variance-issue-20533.rs:40:14
+  --> $DIR/variance-issue-20533.rs:47:14
    |
 LL |         let a = AffineU32(1);
    |             - binding `a` declared here
@@ -34,6 +34,41 @@ LL |         drop(a);
 LL |         drop(x);
    |              - borrow later used here
 
-error: aborting due to 3 previous errors
+error[E0505]: cannot move out of `a` because it is borrowed
+  --> $DIR/variance-issue-20533.rs:53:14
+   |
+LL |         let a = AffineU32(1);
+   |             - binding `a` declared here
+LL |         let x = bat(&a);
+   |                     -- borrow of `a` occurs here
+LL |         drop(a);
+   |              ^ move out of `a` occurs here
+LL |         drop(x);
+   |              - borrow later used here
+   |
+help: consider cloning the value if the performance cost is acceptable
+   |
+LL |         let x = bat(&a).clone();
+   |                        ++++++++
+
+error[E0505]: cannot move out of `a` because it is borrowed
+  --> $DIR/variance-issue-20533.rs:59:14
+   |
+LL |         let a = ClonableAffineU32(1);
+   |             - binding `a` declared here
+LL |         let x = foo(&a);
+   |                     -- borrow of `a` occurs here
+LL |         drop(a);
+   |              ^ move out of `a` occurs here
+LL |         drop(x);
+   |              - borrow later used here
+   |
+help: consider cloning the value if the performance cost is acceptable
+   |
+LL -         let x = foo(&a);
+LL +         let x = foo(a.clone());
+   |
+
+error: aborting due to 5 previous errors
 
 For more information about this error, try `rustc --explain E0505`.