Skip to content
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

Lint map.iter().map(|(_,v)| v) #9376

Closed
hotate29 opened this issue Aug 25, 2022 · 2 comments · Fixed by #9409
Closed

Lint map.iter().map(|(_,v)| v) #9376

hotate29 opened this issue Aug 25, 2022 · 2 comments · Fixed by #9409
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@hotate29
Copy link
Contributor

hotate29 commented Aug 25, 2022

What it does

Warning to iterator operations that are equivalent to .keys() and .values().

Similar lint: https://rust-lang.github.io/rust-clippy/stable/index.html#for_kv_map

Lint Name

iter_kv_map

Category

style

Advantage

Simpler and readable than the original code.

Drawbacks

No response

Example

let map = HashMap::new();

let keys = map.iter().map(|(key, _)| key).collect::<Vec<_>>();
let values = map.iter().map(|(_, value)| value).collect::<Vec<_>>();

Could be written as:

let map = HashMap::new();

let keys = map.keys().collect::<Vec<_>>();
let values = map.values().collect::<Vec<_>>();
@hotate29 hotate29 added the A-lint Area: New lints label Aug 25, 2022
@Alexendoo Alexendoo added the good-first-issue These issues are a good way to get started with Clippy label Aug 25, 2022
@didibear
Copy link

Just mentioning that the lint also works when the key or value is used to create another value.

For example, these two lines are equivalent:

map.iter().map(|(key, _)| key.to_uppercase());

map.keys().map(|key| key.to_uppercase());

@kartva
Copy link
Contributor

kartva commented Aug 28, 2022

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants