ruff
ruff copied to clipboard
False positive with PLE4703 and immediate return / break
Not the biggest deal, but I got a false positive on this with:
λ cat x.py
def foo():
s = {1, 2, 3}
for e in s:
if e >= 3:
s.remove(e)
return
λ ruff check --select PLE x.py --preview
x.py:3:5: PLE4703 Iterated set `s` is modified within the `for` loop
|
1 | def foo():
2 | s = {1, 2, 3}
3 | for e in s:
| _____^
4 | | if e >= 3:
5 | | s.remove(e)
6 | | return
| |__________________^ PLE4703
|
= help: Iterate over a copy of `s`
Found 1 error.
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).
cc @boolean-light in case you're interested, and thanks for the new rule :-)
Thanks!
Struggling not to get sniped into fixing this.
Hmm that makes sense. It would be nice to use the control flow graph for this because I assume there are other situations where the loop exits early (continue, break, raise etc) that aren't covered and they might be more subtle.
Excuse me I'm back :D Thank you, and let me have a look on this.
Sorry, actually I don't think I can implement this one by myself. Is there some kind of support for control flow group in Ruff already?
No, there's not. That's what I meant by my comment. We probably want to wait for a more complete control flow analysis or decided to only support a very small subset of control flow patterns for now.