ruff icon indicating copy to clipboard operation
ruff copied to clipboard

False positive with PLE4703 and immediate return / break

Open hauntsaninja opened this issue 1 year ago • 6 comments

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 :-)

hauntsaninja avatar Apr 01 '24 18:04 hauntsaninja

Thanks!

charliermarsh avatar Apr 01 '24 18:04 charliermarsh

Struggling not to get sniped into fixing this.

charliermarsh avatar Apr 01 '24 18:04 charliermarsh

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.

MichaReiser avatar Apr 02 '24 09:04 MichaReiser

Excuse me I'm back :D Thank you, and let me have a look on this.

boolean-light avatar Apr 03 '24 06:04 boolean-light

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?

boolean-light avatar Apr 08 '24 02:04 boolean-light

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.

MichaReiser avatar Apr 08 '24 06:04 MichaReiser