Skip to content

Catch accidentally exhausting iterators by repeatedly looping through them #2996

@technillogue

Description

@technillogue

Is your feature request related to a problem? Please describe

iterator = (ord(char) for char in string.printable)
for spam in range(10):
      for eggs in iterator: # this won't work as expected and is really hard to catch!
            print(chr(spam*eggs))

Describe the solution you'd like

W\d+: Repeatedly looping through iterator (looping-through-iterator)

Additional context

I realize it's easy to have false positives here, but if we limit to cases where it's clear that it's a non-infinite iterator (e.g. list(iterable), comprehensions, for loops without return/break/continue) I think the main problem is that I could have used range(1) above and that would give a false positive, but that seems like fairly bad style anyway.

Metadata

Metadata

Assignees

Labels

CheckersRelated to a checkerEnhancement ✨Improvement to a componentGood first issueFriendly and approachable by new contributorsHacktoberfestHelp wanted 🙏Outside help would be appreciated, good for new contributorsNeeds specification 🔐Accepted as a potential improvement, and needs to specify edge cases, message names, etc.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions