Skip to content

RUF050 fix needs parentheses for certain multiline expressions #24220

@dscorbett

Description

@dscorbett

Summary

When unnecessary-if (RUF050) keeps the if condition, it can change behavior or introduce a syntax error, if the condition takes multiple lines and wouldn’t be parsed as a single expression outside its original context. The solution is to add parentheses around certain multiline expressions. Example:

$ cat >ruf050.py <<'# EOF'
if (
    id(0)
    + 0
):
    pass
# EOF

$ ruff --isolated check ruf050.py --select RUF050 --preview --fix

error: Fix introduced a syntax error. Reverting all changes.

This indicates a bug in Ruff. If you could open an issue at:

    https://github.com/astral-sh/ruff/issues/new?title=%5BFix%20error%5D

...quoting the contents of `ruf050.py`, the rule codes RUF050, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!

error[RUF050]: Empty `if` statement
 --> ruf050.py:1:1
  |
1 | / if (
2 | |     id(0)
3 | |     + 0
4 | | ):
5 | |     pass
  | |________^
  |
help: Remove the `if` statement
  - if (
  -     id(0)
1 + id(0)
2 |     + 0
  - ):
  -     pass

Found 1 error.
[*] 1 fixable with the `--fix` option.

Version

ruff 0.15.8 (c2a8815 2026-03-26)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationspreviewRelated to preview mode features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions