-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #126226 - gurry:125325-improve-closure-arg-sugg, r=ol…
…i-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.
- Loading branch information
Showing
3 changed files
with
126 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
28 changes: 28 additions & 0 deletions
28
tests/ui/closures/wrong-closure-arg-suggestion-125325.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |