-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Suggest mutable references for FnMut closure arguments #161656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rust-bors
merged 2 commits into
rust-lang:main
from
chenyukang:yukang-fix-118843-suggest-mut-fnmut-reference
Sep 5, 2026
+104
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,11 @@ | ||
| //! Do not suggest a mutable reference when the root obligation genuinely requires `Fn`. | ||
|
|
||
| fn requires_fn<F: Fn()>(_: F) {} | ||
|
|
||
| fn main() { | ||
| let mut value = 0; | ||
| let mut func = || value += 1; | ||
| //~^ ERROR expected a closure that implements the `Fn` trait | ||
|
|
||
| requires_fn(&func); | ||
| } |
23 changes: 23 additions & 0 deletions
23
tests/ui/closures/fnmut-shared-reference-requires-fn.stderr
This file contains hidden or 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,23 @@ | ||
| error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut` | ||
| --> $DIR/fnmut-shared-reference-requires-fn.rs:7:20 | ||
| | | ||
| LL | let mut func = || value += 1; | ||
| | ^^ ----- closure is `FnMut` because it mutates the variable `value` here | ||
| | | | ||
| | this closure implements `FnMut`, not `Fn` | ||
| ... | ||
| LL | requires_fn(&func); | ||
| | ----------- ---- the requirement to implement `Fn` derives from here | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = note: required for `&{closure@$DIR/fnmut-shared-reference-requires-fn.rs:7:20: 7:22}` to implement `Fn()` | ||
| note: required by a bound in `requires_fn` | ||
| --> $DIR/fnmut-shared-reference-requires-fn.rs:3:19 | ||
| | | ||
| LL | fn requires_fn<F: Fn()>(_: F) {} | ||
| | ^^^^ required by this bound in `requires_fn` | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0525`. |
13 changes: 13 additions & 0 deletions
13
tests/ui/closures/fnmut-shared-reference-suggestion-issue-118843.fixed
This file contains hidden or 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,13 @@ | ||
| //@ run-rustfix | ||
|
|
||
| //! Regression test for https://github.com/rust-lang/rust/issues/118843. | ||
| //! A shared reference to an `FnMut` closure should suggest a mutable reference. | ||
|
|
||
| fn main() { | ||
| let mut value = 0; | ||
| let mut func = |increment: usize| value += increment; | ||
| //~^ ERROR expected a closure that implements the `Fn` trait | ||
|
|
||
| (0..100).for_each(&mut func); | ||
| //~^ HELP consider changing this borrow's mutability | ||
| } |
13 changes: 13 additions & 0 deletions
13
tests/ui/closures/fnmut-shared-reference-suggestion-issue-118843.rs
This file contains hidden or 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,13 @@ | ||
| //@ run-rustfix | ||
|
|
||
| //! Regression test for https://github.com/rust-lang/rust/issues/118843. | ||
| //! A shared reference to an `FnMut` closure should suggest a mutable reference. | ||
|
|
||
| fn main() { | ||
| let mut value = 0; | ||
| let mut func = |increment: usize| value += increment; | ||
| //~^ ERROR expected a closure that implements the `Fn` trait | ||
|
|
||
| (0..100).for_each(&func); | ||
| //~^ HELP consider changing this borrow's mutability | ||
| } |
24 changes: 24 additions & 0 deletions
24
tests/ui/closures/fnmut-shared-reference-suggestion-issue-118843.stderr
This file contains hidden or 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,24 @@ | ||
| error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut` | ||
| --> $DIR/fnmut-shared-reference-suggestion-issue-118843.rs:8:20 | ||
| | | ||
| LL | let mut func = |increment: usize| value += increment; | ||
| | ^^^^^^^^^^^^^^^^^^ ----- closure is `FnMut` because it mutates the variable `value` here | ||
| | | | ||
| | this closure implements `FnMut`, not `Fn` | ||
| ... | ||
| LL | (0..100).for_each(&func); | ||
| | -------- ---- the requirement to implement `Fn` derives from here | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = note: required for `&{closure@$DIR/fnmut-shared-reference-suggestion-issue-118843.rs:8:20: 8:38}` to implement `FnMut(usize)` | ||
| note: required by a bound in `for_each` | ||
| --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
| help: consider changing this borrow's mutability | ||
| | | ||
| LL | (0..100).for_each(&mut func); | ||
| | +++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0525`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if this case is in scope, but if the borrow comes from a macro body the suggestion lands inside the macro definition:
since its
MachineApplicableand the new test hasrun-rustfix, cargo fix would rewrite the macro. master emits the same E0525 with no help line, so i think this comes in with the new call site rather than being pre existing.would a
from_expansioncheck on the arg be enough to rule it out?verified both commits on master 5db7f4b
View changes since the review