Skip to content

for_kv_map false negative when using iter #16822

@xtqqczze

Description

@xtqqczze

Summary

The lint is not triggered when iterating over a HashMap with iter{_mut} and destructuring the key as _, even though only the values are used.

Lint Name

for_kv_map

Reproducer

I tried this code:

pub fn m1(mut x: HashMap<usize, usize>) {
    for (_, v) in x.iter_mut() {
        black_box(v);
    }
}

https://rust.godbolt.org/z/qshMWz8xr

I expected to see this happen:

warning: you seem to want to iterate on a map's values
  --> <source>:6:19
   |
13 |     for (_, v) in x.iter_mut() {
   |                   ^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.94.0/index.html#for_kv_map
   = note: `#[warn(clippy::for_kv_map)]` on by default
help: use the corresponding method
   |
13 -     for (_, y) in x.iter_mut() {
13 +     for v in x.values_mut() {
   |

Instead, this happened:

No warning emitted.

Version

nightly

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions