Closed
Description
Example:
def get_options(option1, option2):
options = []
if option1:
options = options + ["option1"]
if option2:
options = options + ["option2"]
return options
Output:
$ ruff check --isolated --select RET ret_test.py
ret_test2.py:16:12: RET504 Unnecessary variable assignment before `return` statement
Found 1 error.
In this case, there is no unnecessary variable assignment that could be removed and cannot do an earlier return
as need to get through all the conditionals.
That said, the error does not trigger if one were to instead use an in-place operator such as +=
:
def get_options(option1, option2):
options = []
if option1:
options += ["option1"]
if option2:
options += ["option2"]
return options
Version:
$ ruff --version
ruff 0.0.247