-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New Checker for exhausted iterator in nested loops #10570
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
base: main
Are you sure you want to change the base?
New Checker for exhausted iterator in nested loops #10570
Conversation
β¦emove ambiguity about which empty line triggers the warning.
Handles iterator reassignment, shadowing out of scope user defined iterators control flow statements conditional statements, break, continue within loops
Handles iterator reassignment, shadowing out of scope user defined iterators control flow statements conditional statements, break, continue within loops
for more information, see https://pre-commit.ci
Codecov Reportβ Patch coverage is
β Your patch check has failed because the patch coverage (98.76%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #10570 +/- ##
==========================================
+ Coverage 95.90% 95.91% +0.01%
==========================================
Files 176 177 +1
Lines 19453 19534 +81
==========================================
+ Hits 18656 18736 +80
- Misses 797 798 +1
π New features to boost your workflow:
|
π€ Effect of this PR on checked open source code: π€ Effect on black:
Effect on music21:
Effect on django:
This comment was generated for commit d90e952 |
Thank you for working on this. Looking at the primer we need to detect when Stopiteration are caught so we do not raise in this case. |
The first example from Django is also a false positive given that the outer loop |
Thank you for the review. I understand the false positives are due to checker not being smart enough to detect the flow and try catch blocks. I will work on enhancing it to detect the break, return statements for unconditional exits by adding control flow analysis for inner and outer loops. |
Type of Changes
Description
The pull request introduces a new checker, looping-through-iterator (W4801), to detect a specific, high-confidence bug pattern: an iterator defined in an outer scope being exhausted by reuse in a nested loop.
This pattern can lead to silent bugs where an inner loop runs completely on the first iteration of an outer loop and then does nothing on all subsequent iterations. Following the maintainers' advice to start small, this checker is intentionally focused on this nested-loop scenario to ensure high accuracy and avoid false positives.
Refs #2996
The checker specifically looks for an iterator being consumed in a loop that is nested inside at least one other loop. It correctly handles cases where the iterator is re-defined inside the outer loop (shadowing).
It does not yet detect sequential reuse of an iterator in non-nested loops. This could be a potential enhancement in a future PR.
Running this checker locally against the primer projects successfully identified 4 instances of this bug pattern in established repositories, including Django, music21, and Black, demonstrating its real-world utility.
Closes #XXXX