Skip to content

Suggestion for "error[E0507]: cannot move out of a shared reference" suggests removing the wrong borrow #132806

Closed
@unflxw

Description

@unflxw

I tried this code (playground):

use std::collections::HashMap;

fn main() {
    HashMap::<String, i32>::new().iter().filter( |&(&k, &v)| { true });
}

I expected to see this happen:

error[E0507]: cannot move out of a shared reference
[...]
help: consider removing the borrow
  |
4 -     HashMap::<String, i32>::new().iter().filter( |&(&k, &v)| { true });
4 +     HashMap::<String, i32>::new().iter().filter( |&(k, &v)| { true });
  |

Instead, this happened:

error[E0507]: cannot move out of a shared reference
[...]
help: consider removing the borrow
  |
4 -     HashMap::<String, i32>::new().iter().filter( |&(&k, &v)| { true });
4 +     HashMap::<String, i32>::new().iter().filter( |(&k, &v)| { true });
  |

Diff between the two (red is actual, green is expected):

- 4 +     HashMap::<String, i32>::new().iter().filter( |(&k, &v)| { true });
+ 4 +     HashMap::<String, i32>::new().iter().filter( |&(k, &v)| { true });

That is, it is suggesting removing the borrow on the tuple, which is not the correct borrow to remove; instead, it should suggest removing the borrow on k, the element that isn't Copy.

Happens in playground nightly: 1.8.0-nightly (2024-11-08 59cec72)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions