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

New lint suggestion: detect HashMap/Set::iter() followed by take_while()/skip_while()/map_while(). #7883

Open
conradoplg opened this issue Oct 25, 2021 · 3 comments
Labels
A-lint Area: New lints

Comments

@conradoplg
Copy link

What it does

Detects usage of HashMap/Set::iter() (or other iterator) followed by take_while()/skip_while()/map_while().

Categories (optional)

  • Kind: suspicious, maybe correctness?

What is the advantage of the recommended code over the original code

iter() in HashMap/Set iterates in arbitrary order, and _while() methods usually make sense where is there some kind of order in the iterator. This would catch using a e.g. HashMap where a sorted structure would be required.

Drawbacks

False positives, but it seems unlikely.

Example

Note sure if anything useful can be suggested other than detecting the issue.

@conradoplg conradoplg added the A-lint Area: New lints label Oct 25, 2021
@teor2345
Copy link
Contributor

Iterator::last, nth, position, rposition, rev, and step_by are also suspicious with HashMap and HashSet, because element positions are arbitrary.

(A possible substitute for last and nth is next, because they all get an arbitrary element.)

@teor2345
Copy link
Contributor

teor2345 commented Nov 2, 2021

rustc has a similar internal-only lint: rust-lang/rust#89558

@emilk
Copy link

emilk commented Feb 4, 2023

I would be very interested in a clippy against any call to .iter() or .drain() on HashMap/Set, no matter what you do with the result. The arbitrary iteration order has been the source of many bugs. Maybe I should make that its own ticket?

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

No branches or pull requests

3 participants