about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorMaybe Waffle <waffle.lapkin@gmail.com>2023-04-19 18:19:55 +0000
committerMaybe Waffle <waffle.lapkin@gmail.com>2023-04-19 19:13:35 +0000
commit102c0af5a75354acb21ebceb06765ae8d54a8270 (patch)
treebd3d2cd62c93143f618259289a0ebdbfeff96ad6 /tests
parentcfe5c3ca6cb0f0b9ffa3f08c42ed12e39905fdb5 (diff)
downloadrust-102c0af5a75354acb21ebceb06765ae8d54a8270.tar.gz
rust-102c0af5a75354acb21ebceb06765ae8d54a8270.zip
Add suggestion to use a closure arg instead of a capture on bck error
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/borrowck/issue-109271-pass-self-into-closure.fixed39
-rw-r--r--tests/ui/borrowck/issue-109271-pass-self-into-closure.rs1
-rw-r--r--tests/ui/borrowck/issue-109271-pass-self-into-closure.stderr20
3 files changed, 55 insertions, 5 deletions
diff --git a/tests/ui/borrowck/issue-109271-pass-self-into-closure.fixed b/tests/ui/borrowck/issue-109271-pass-self-into-closure.fixed
new file mode 100644
index 00000000000..4a8831dab95
--- /dev/null
+++ b/tests/ui/borrowck/issue-109271-pass-self-into-closure.fixed
@@ -0,0 +1,39 @@
+// run-rustfix
+#![allow(unused)]
+struct S;
+
+impl S {
+    fn call(&mut self, f: impl FnOnce((), &mut Self)) {
+        // change state or something ...
+        f((), self);
+        // change state or something ...
+    }
+
+    fn get(&self) {}
+    fn set(&mut self) {}
+}
+
+fn main() {
+    let mut v = S;
+
+    v.call(|(), this: &mut S| this.get());
+    //~^ error: cannot borrow `v` as mutable because it is also borrowed as immutable
+    v.call(|(), this: &mut S| this.set());
+    //~^ error: cannot borrow `v` as mutable more than once at a time
+    //~| error: cannot borrow `v` as mutable more than once at a time
+
+    v.call(|(), this: &mut S| {
+        //~^ error: cannot borrow `v` as mutable more than once at a time
+        //~| error: cannot borrow `v` as mutable more than once at a time
+
+        _ = this;
+        this.set();
+        this.get();
+        S::get(&this);
+
+        use std::ops::Add;
+        let v = 0u32;
+        _ = v + v;
+        _ = v.add(3);
+    });
+}
diff --git a/tests/ui/borrowck/issue-109271-pass-self-into-closure.rs b/tests/ui/borrowck/issue-109271-pass-self-into-closure.rs
index 16167914512..fcd855f862d 100644
--- a/tests/ui/borrowck/issue-109271-pass-self-into-closure.rs
+++ b/tests/ui/borrowck/issue-109271-pass-self-into-closure.rs
@@ -1,3 +1,4 @@
+// run-rustfix
 #![allow(unused)]
 struct S;
 
diff --git a/tests/ui/borrowck/issue-109271-pass-self-into-closure.stderr b/tests/ui/borrowck/issue-109271-pass-self-into-closure.stderr
index 8abbecad02a..25974e0d008 100644
--- a/tests/ui/borrowck/issue-109271-pass-self-into-closure.stderr
+++ b/tests/ui/borrowck/issue-109271-pass-self-into-closure.stderr
@@ -1,27 +1,29 @@
 error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
-  --> $DIR/issue-109271-pass-self-into-closure.rs:18:5
+  --> $DIR/issue-109271-pass-self-into-closure.rs:19:5
    |
 LL |     v.call(|(), this: &mut S| v.get());
    |     ^^----^------------------^-^^^^^^^
    |     | |    |                  |
    |     | |    |                  first borrow occurs due to use of `v` in closure
+   |     | |    |                  help: try using the closure argument: `this`
    |     | |    immutable borrow occurs here
    |     | immutable borrow later used by call
    |     mutable borrow occurs here
 
 error[E0499]: cannot borrow `v` as mutable more than once at a time
-  --> $DIR/issue-109271-pass-self-into-closure.rs:20:5
+  --> $DIR/issue-109271-pass-self-into-closure.rs:21:5
    |
 LL |     v.call(|(), this: &mut S| v.set());
    |     ^^----^------------------^-^^^^^^^
    |     | |    |                  |
    |     | |    |                  first borrow occurs due to use of `v` in closure
+   |     | |    |                  help: try using the closure argument: `this`
    |     | |    first mutable borrow occurs here
    |     | first borrow later used by call
    |     second mutable borrow occurs here
 
 error[E0499]: cannot borrow `v` as mutable more than once at a time
-  --> $DIR/issue-109271-pass-self-into-closure.rs:20:12
+  --> $DIR/issue-109271-pass-self-into-closure.rs:21:12
    |
 LL |     v.call(|(), this: &mut S| v.set());
    |     -------^^^^^^^^^^^^^^^^^^---------
@@ -32,7 +34,7 @@ LL |     v.call(|(), this: &mut S| v.set());
    |     first mutable borrow occurs here
 
 error[E0499]: cannot borrow `v` as mutable more than once at a time
-  --> $DIR/issue-109271-pass-self-into-closure.rs:24:5
+  --> $DIR/issue-109271-pass-self-into-closure.rs:25:5
    |
 LL |       v.call(|(), this: &mut S| {
    |       ^ ---- ------------------ first mutable borrow occurs here
@@ -49,9 +51,17 @@ LL | |         v.set();
 LL | |         _ = v.add(3);
 LL | |     });
    | |______^ second mutable borrow occurs here
+   |
+help: try using the closure argument
+   |
+LL ~         _ = this;
+LL ~         this.set();
+LL ~         this.get();
+LL ~         S::get(&this);
+   |
 
 error[E0499]: cannot borrow `v` as mutable more than once at a time
-  --> $DIR/issue-109271-pass-self-into-closure.rs:24:12
+  --> $DIR/issue-109271-pass-self-into-closure.rs:25:12
    |
 LL |       v.call(|(), this: &mut S| {
    |       - ---- ^^^^^^^^^^^^^^^^^^ second mutable borrow occurs here