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

Implement new lint iter_over_hash_type #11791

Merged
merged 5 commits into from
Nov 14, 2023

Conversation

Jacherr
Copy link
Contributor

@Jacherr Jacherr commented Nov 11, 2023

Implements and fixes #11788

This PR adds a new restriction lint iter_over_hash_type which prevents Hash-types (that is, HashSet and HashMap) from being used as the iterator in for loops.

The justification for this is because in Hash-based types, the ordering of items is not guaranteed and may vary between executions of the same program on the same hardware. In addition, it reduces readability due to the unclear iteration order.

The implementation of this lint also ensures the following:

  • Calls to HashMap::keys, HashMap::values, and HashSet::iter are also denied when used in for loops,
  • When this expression is used in procedural macros, it is not linted/denied.

changelog: add new iter_over_hash_type lint to prevent unordered iterations through hashed data structures

@rustbot
Copy link
Collaborator

rustbot commented Nov 11, 2023

r? @Jarcho

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 11, 2023
Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall fine other than some small changes. It would be nice if this could handle adapters being used (e.g. map), but that can be done as a future extension.

@rustbot author

Comment on lines 49 to 53
&& (match_def_path(cx, did, &HASHMAP_KEYS)
|| match_def_path(cx, did, &HASHMAP_VALUES)
|| match_def_path(cx, did, &HASHSET_ITER_TY)
|| is_type_diagnostic_item(cx, ty, sym::HashMap)
|| is_type_diagnostic_item(cx, ty, sym::HashSet))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing HashSet::IterMut, HashMap::Iter, HashMap::IterMut and HashMap::ValuesMut. Drain for both collections should probably also be caught.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashSet::IterMut doesn't appear to exist, but I'll add the rest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it does not.

///
/// ### Why is this bad?
/// Because hash types are unordered, when iterated through such as in a for loop, the values are returned in
/// a pseudo-random order. As a result, on redundant systems this may cause inconsistencies and anomalies.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: an undefined order

'pseudo-random' is not technically wrong, but very misleading.

cx,
ITER_OVER_HASH_TYPE,
expr.span,
"iterating over unordered hash-based type",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: iteration over

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Nov 11, 2023
@Jacherr
Copy link
Contributor Author

Jacherr commented Nov 11, 2023

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Nov 11, 2023
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like tests with type aliases as well, like FxHashMap. This should work since you use the typeck table but it should be linted

@Jarcho
Copy link
Contributor

Jarcho commented Nov 14, 2023

Thank you. @bors r+

@bors
Copy link
Collaborator

bors commented Nov 14, 2023

📌 Commit f8ea496 has been approved by Jarcho

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Nov 14, 2023

⌛ Testing commit f8ea496 with merge 0c42e45...

@bors
Copy link
Collaborator

bors commented Nov 14, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Jarcho
Pushing 0c42e45 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint for iterating over the keys in an unordered collection such as HashSet or HashMap
5 participants