Skip to content

cannot assign to data in an index of HashMap fixit generates invalid code #16076

Open
@narpfel

Description

@narpfel

The autofix for map[&key] = value; generates invalid code:

use std::collections::HashMap;

fn main() {
    let mut map = HashMap::<usize, usize>::new();
    map[&42] = 27;
}

==>

use std::collections::HashMap;

fn main() {
    let mut map = HashMap::<usize, usize>::new();
    let val = map.insert(get_mut(entry(&42, p(|val| { *val.or_insert(27); }));
}

rust-analyzer version: rust-analyzer 1 (e402c49 2023-12-01) (installed from the Arch Linux repos)

rustc version:

$ rustc -vV
rustc 1.76.0-nightly (06e02d5b2 2023-12-09)
binary: rustc
commit-hash: 06e02d5b259c1e88cbf0c74366d9e0a4c7cfd6d9
commit-date: 2023-12-09
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.5

(installed via rustup)

relevant settings: Editor used is nvim version NVIM v0.10.0-dev-1807+g096211a87b (current git head) using current git head of nvim-lspconfig. The command used to trigger the refactor is :lua vim.lsp.buf.code_action() and then selecting “to modify a HashMap [...]” while in normal mode with the cursor above the line map[&42] = 27;.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsdiagnostics / error reportingA-flycheckissues with flycheck a.k.a. "check on save"C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions