Skip to content

Lint for iterating over the keys in an unordered collection such as HashSet or HashMap #11788

Closed
@kayabaNerve

Description

@kayabaNerve

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.

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions