Skip to content

RUF046 fix introduces a syntax error when the cast expression contains a newline #15226

@dscorbett

Description

@dscorbett

The fix for unnecessary-cast-to-int (RUF046) can crash Ruff in version 0.8.5 or change behavior when the cast expression contains a newline.

Here is a crash:

$ cat ruf046_1.py
int(1
& 1)

$ ruff check --isolated --preview --select RUF046 ruf046_1.py --diff

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 `ruf046_1.py`, the rule codes RUF046, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!

Here is a syntax error:

$ cat ruf046_2.py
int(1
* 1)

$ ruff check --isolated --preview --select RUF046 ruf046_2.py --fix
Found 1 error (1 fixed, 0 remaining).

$ cat ruf046_2.py
1
* 1

$ python ruf046_2.py
  File "ruf046_2.py", line 2
    * 1
    ^^^
SyntaxError: can't use starred expression here

Here is a runtime behavior change:

$ cat ruf046_3.py
x = int(1
+ 1)
print(x)

$ python ruf046_3.py
2

$ ruff check --isolated --preview --select RUF046 ruf046_3.py --fix
Found 1 error (1 fixed, 0 remaining).

$ cat ruf046_3.py
x = 1
+ 1
print(x)

$ python ruf046_3.py
1

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingfixesRelated to suggested fixes for violations

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions