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

Rule: Flake8-bugbear B038 " detect changes to iterable object of loop" #9511

Closed
mimre25 opened this issue Jan 14, 2024 · 5 comments · Fixed by #9578
Closed

Rule: Flake8-bugbear B038 " detect changes to iterable object of loop" #9511

mimre25 opened this issue Jan 14, 2024 · 5 comments · Fixed by #9578
Assignees
Labels
accepted Ready for implementation rule Implementing or modifying a lint rule

Comments

@mimre25
Copy link
Contributor

mimre25 commented Jan 14, 2024

I've added a new rule to flake8-bugbear.

In brief, the rule detects bugs like the following:

some_list = [1,2,3]
for i in some_list:
  if i % 2 == 0:
    some_list.remove(i)
  print(i)

(More context at github.com/PyCQA/flake8-bugbear/issues/445)

I'd be happy to contribute if its desired to implement this rule in ruff as well 🙂

@charliermarsh
Copy link
Member

Definitely happy to include in Ruff, would welcome a contribution!

@charliermarsh charliermarsh added rule Implementing or modifying a lint rule accepted Ready for implementation labels Jan 14, 2024
@AlexWaygood AlexWaygood changed the title Rule: Flake8-bugbar B038 " detect changes to iterable object of loop" Rule: Flake8-bugbear B038 " detect changes to iterable object of loop" Jan 14, 2024
@mimre25
Copy link
Contributor Author

mimre25 commented Jan 14, 2024

Sounds good - I hope I can get the PR during this week 🙂

@charliermarsh
Copy link
Member

Awesome :) I will assign you to avoid others hopping on it accidentally, but just LMK if you find yourself unable to get to it.

@mimre25
Copy link
Contributor Author

mimre25 commented Jan 16, 2024

FYI: I got a prototype working, but with flake8-bugbear we already got reports about some oversights leading to false positives - I'm looking into making it more solid - just wanted to give a heads up that I'm getting there 🙂

@mimre25
Copy link
Contributor Author

mimre25 commented Jan 19, 2024

PR is out: #9578

charliermarsh pushed a commit that referenced this issue Apr 11, 2024
## Summary
This PR adds the implementation for the current
[flake8-bugbear](https://github.com/PyCQA/flake8-bugbear)'s B038 rule.
The B038 rule checks for mutation of loop iterators in the body of a for
loop and alerts when found.

Rational: 
Editing the loop iterator can lead to undesired behavior and is probably
a bug in most cases.

Closes #9511.

Note there will be a second iteration of B038 implemented in
`flake8-bugbear` soon, and this PR currently only implements the weakest
form of the rule.
I'd be happy to also implement the further improvements to B038 here in
ruff 🙂
See PyCQA/flake8-bugbear#454 for more
information on the planned improvements.

## Test Plan
Re-using the same test file that I've used for `flake8-bugbear`, which
is included in this PR (look for the `B038.py` file).


Note: this is my first time using `rust` (beside `rustlings`) - I'd be
very happy about thorough feedback on what I could've done better
:slightly_smiling_face: - Bring it on :grinning:
Glyphack pushed a commit to Glyphack/ruff that referenced this issue Apr 12, 2024
…l-sh#9578)

## Summary
This PR adds the implementation for the current
[flake8-bugbear](https://github.com/PyCQA/flake8-bugbear)'s B038 rule.
The B038 rule checks for mutation of loop iterators in the body of a for
loop and alerts when found.

Rational: 
Editing the loop iterator can lead to undesired behavior and is probably
a bug in most cases.

Closes astral-sh#9511.

Note there will be a second iteration of B038 implemented in
`flake8-bugbear` soon, and this PR currently only implements the weakest
form of the rule.
I'd be happy to also implement the further improvements to B038 here in
ruff 🙂
See PyCQA/flake8-bugbear#454 for more
information on the planned improvements.

## Test Plan
Re-using the same test file that I've used for `flake8-bugbear`, which
is included in this PR (look for the `B038.py` file).


Note: this is my first time using `rust` (beside `rustlings`) - I'd be
very happy about thorough feedback on what I could've done better
:slightly_smiling_face: - Bring it on :grinning:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants