astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
28.92k stars 938 forks source link

New rule request: Local variable might be referenced before assignment #6902

Open Garrett-R opened 10 months ago

Garrett-R commented 10 months ago

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
charliermarsh commented 10 months ago

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

Garrett-R commented 10 months ago

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

inoa-jboliveira commented 8 months ago

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.