ruff icon indicating copy to clipboard operation
ruff copied to clipboard

New rule request: Local variable might be referenced before assignment

Open Garrett-R opened this issue 1 year ago • 3 comments

It would be awesome if Ruff could catch an error (like PyCharm does) for code like this:

def func(x: bool) -> int:
    if x:
        y = 1
    return y

Garrett-R avatar Aug 26 '23 19:08 Garrett-R

Makes sense. It's not trivial to fix because it requires supporting full branch analysis, but it would be good to catch of course.

charliermarsh avatar Aug 26 '23 21:08 charliermarsh

Yeah, seems really complicated, as does all this linter magic haha

Garrett-R avatar Aug 26 '23 22:08 Garrett-R

At least pycharm and possibly other IDEs static code analysis can catch this bug. I was also hoping to also find this rule in Ruff. I thought F823 would catch this, but it doesn't.

inoa-jboliveira avatar Oct 23 '23 21:10 inoa-jboliveira