Skip to content

Malformed suggestion for clippy::iter_kv_map when using vec! in map #16340

@gikf

Description

@gikf

Summary

Clippy gives malformed suggestion for fixing clippy::iter_kv_map, when vec! macro is used in the map.

Reproducer

Code:

use std::collections::HashMap;

pub fn main() {
    let hm: HashMap<&str, &str> = HashMap::new();
    let _ = hm.iter().map(|(key, _)| vec![key]);
}

Issue occurs specifically when vec! is present. Using ie.:

    hm.iter().map(|(key, _)| Vec::from([key]));

doesn't trigger the problem.

Current output:

warning: iterating on a map's keys
 --> src/main.rs:5:13
  |
5 |     let _ = hm.iter().map(|(key, _)| vec![key]);
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_kv_map
  = note: `#[warn(clippy::iter_kv_map)]` on by default
help: try
  |
5 ~     let _ = hm.keys().map(|key| <[_]>::into_vec(
6 +             // Using the intrinsic produces a dramatic improvement in stack usage for
7 +             // unoptimized programs using this code path to construct large Vecs.
8 +             $crate::boxed::box_new([$($x),+])
9 ~         ));
  |

Desired output:

warning: iterating on a map's keys
 --> src/main.rs:5:13
  |
5 |     let _ = hm.iter().map(|(key, _)| vec![key]);
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hm.keys().map(|key| vec![key])`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_kv_map
  = note: `#[warn(clippy::iter_kv_map)]` on by default

Version

rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions