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 for iterating over the keys in an unordered collection such as HashSet or HashMap #11788

Closed
kayabaNerve opened this issue Nov 10, 2023 · 3 comments · Fixed by #11791
Closed
Assignees
Labels
A-lint Area: New lints

Comments

@kayabaNerve
Copy link

kayabaNerve commented Nov 10, 2023

What it does

A (possibly pedantic) lint for iterating over a HashSet/HashMap would be appreciated. Given these do not stable iteration orders across program instances, any usage of them in redundant systems may cause differing results and inconsistencies. They also have an unclear iteration order (not lexicographical to keys nor by order) which may give adverse effects in a variety of other use cases.

In effect, the same as rust-lang/rust#92584 (which I would appreciate if anyone could give me insight on how to fork/run over my own project now, possibly via dylint).

Advantage

No response

Drawbacks

No response

Example

for (key, value) in my_map {}

Could be written as:

let mut keys = my_map.keys().clone().collect::<Vec<_>();
keys.sort();
for key in keys {
  let value = &my_map[value];
}

Please note I don't actually endorse the suggested alternative. My usages of map iteration frequently have context which show how iteration should occur. Without said context, my example is forced to create a slow but safe migration for this lint topic.

@Jarcho
Copy link
Contributor

Jarcho commented Nov 10, 2023

This is definitely a restriction lint, but it does have it's uses.

@Jacherr
Copy link
Contributor

Jacherr commented Nov 10, 2023

@rustbot claim

@kayabaNerve
Copy link
Author

Thank you @Jacherr!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants