Skip to content

iter_kv_map: recognize references on maps as well #14596

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
merged 1 commit into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/iter_kv_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(super) fn check<'tcx>(
(PatKind::Binding(ann, _, key, _), value) if pat_is_wild(cx, value, m_arg) => ("key", ann, key),
_ => return,
}
&& let ty = cx.typeck_results().expr_ty(recv)
&& let ty = cx.typeck_results().expr_ty_adjusted(recv).peel_refs()
&& (is_type_diagnostic_item(cx, ty, sym::HashMap) || is_type_diagnostic_item(cx, ty, sym::BTreeMap))
{
let mut applicability = rustc_errors::Applicability::MachineApplicable;
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/iter_kv_map.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,18 @@ fn msrv_1_54() {
let _ = map.values().map(|v| v + 2).collect::<Vec<_>>();
//~^ iter_kv_map
}

fn issue14595() {
pub struct Foo(BTreeMap<String, i32>);

impl AsRef<BTreeMap<String, i32>> for Foo {
fn as_ref(&self) -> &BTreeMap<String, i32> {
&self.0
}
}

let map = Foo(BTreeMap::default());

let _ = map.as_ref().values().copied().collect::<Vec<_>>();
//~^ iter_kv_map
}
15 changes: 15 additions & 0 deletions tests/ui/iter_kv_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,18 @@ fn msrv_1_54() {
let _ = map.iter().map(|(_, v)| v + 2).collect::<Vec<_>>();
//~^ iter_kv_map
}

fn issue14595() {
pub struct Foo(BTreeMap<String, i32>);

impl AsRef<BTreeMap<String, i32>> for Foo {
fn as_ref(&self) -> &BTreeMap<String, i32> {
&self.0
}
}

let map = Foo(BTreeMap::default());

let _ = map.as_ref().iter().map(|(_, v)| v).copied().collect::<Vec<_>>();
//~^ iter_kv_map
}
8 changes: 7 additions & 1 deletion tests/ui/iter_kv_map.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,11 @@ error: iterating on a map's values
LL | let _ = map.iter().map(|(_, v)| v + 2).collect::<Vec<_>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `map.values().map(|v| v + 2)`

error: aborting due to 38 previous errors
error: iterating on a map's values
--> tests/ui/iter_kv_map.rs:185:13
|
LL | let _ = map.as_ref().iter().map(|(_, v)| v).copied().collect::<Vec<_>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `map.as_ref().values()`

error: aborting due to 39 previous errors

Loading