about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2024-06-17 11:28:53 +0200
committerGitHub <noreply@github.com>2024-06-17 11:28:53 +0200
commit9f5e2e314c3a6cf321c193506e9ea9f4b3a920b1 (patch)
treebf0dfd8b825f42c8e6e1f412e019e37e6eef1e7a /tests
parentfd7eefc2753e867053a1c567a7b504ae308e3f85 (diff)
parente47ea38da854eea3aebd35b43e3a4dfc7d25c651 (diff)
downloadrust-9f5e2e314c3a6cf321c193506e9ea9f4b3a920b1.tar.gz
rust-9f5e2e314c3a6cf321c193506e9ea9f4b3a920b1.zip
Rollup merge of #126226 - gurry:125325-improve-closure-arg-sugg, r=oli-obk
Make suggestion to change `Fn` to `FnMut` work with methods as well

Fixes #125325

The issue occurred because the code that emitted the suggestion to change `Fn` to `FnMut` worked only for function calls  and not method calls. This PR makes it work with methods as well.
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/closures/wrong-closure-arg-suggestion-125325.rs29
-rw-r--r--tests/ui/closures/wrong-closure-arg-suggestion-125325.stderr28
2 files changed, 57 insertions, 0 deletions
diff --git a/tests/ui/closures/wrong-closure-arg-suggestion-125325.rs b/tests/ui/closures/wrong-closure-arg-suggestion-125325.rs
new file mode 100644
index 00000000000..ce575697cf6
--- /dev/null
+++ b/tests/ui/closures/wrong-closure-arg-suggestion-125325.rs
@@ -0,0 +1,29 @@
+// Regression test for #125325
+
+// Tests that we suggest changing an `impl Fn` param
+// to `impl FnMut` when the provided closure arg
+// is trying to mutate the closure env.
+// Ensures that it works that way for both
+// functions and methods
+
+struct S;
+
+impl S {
+    fn assoc_func(&self, _f: impl Fn()) -> usize {
+        0
+    }
+}
+
+fn func(_f: impl Fn()) -> usize {
+    0
+}
+
+fn test_func(s: &S) -> usize {
+    let mut x = ();
+    s.assoc_func(|| x = ());
+    //~^ cannot assign to `x`, as it is a captured variable in a `Fn` closure
+    func(|| x = ())
+    //~^ cannot assign to `x`, as it is a captured variable in a `Fn` closure
+}
+
+fn main() {}
diff --git a/tests/ui/closures/wrong-closure-arg-suggestion-125325.stderr b/tests/ui/closures/wrong-closure-arg-suggestion-125325.stderr
new file mode 100644
index 00000000000..e0cce8c4b31
--- /dev/null
+++ b/tests/ui/closures/wrong-closure-arg-suggestion-125325.stderr
@@ -0,0 +1,28 @@
+error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
+  --> $DIR/wrong-closure-arg-suggestion-125325.rs:23:21
+   |
+LL |     fn assoc_func(&self, _f: impl Fn()) -> usize {
+   |                              --------- change this to accept `FnMut` instead of `Fn`
+...
+LL |     s.assoc_func(|| x = ());
+   |       --------------^^^^^^-
+   |       |          |  |
+   |       |          |  cannot assign
+   |       |          in this closure
+   |       expects `Fn` instead of `FnMut`
+
+error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
+  --> $DIR/wrong-closure-arg-suggestion-125325.rs:25:13
+   |
+LL | fn func(_f: impl Fn()) -> usize {
+   |             --------- change this to accept `FnMut` instead of `Fn`
+...
+LL |     func(|| x = ())
+   |     ---- -- ^^^^^^ cannot assign
+   |     |    |
+   |     |    in this closure
+   |     expects `Fn` instead of `FnMut`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0594`.