Skip to content

RUF055 ignores backslashes in the repl argument of re.sub #14757

@dscorbett

Description

@dscorbett

In Ruff 0.8.1, unnecessary-regular-expression (RUF055) reports a false positive for re.sub when the replacement string contains a backslash.

$ cat ruf055.py
import re
print(re.sub(" ", r"\n", "a b"))

$ python ruf055.py
a
b

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

$ cat ruf055.py
import re
print("a b".replace(" ", "\\n"))

$ python ruf055.py
a\nb

This issue was mentioned in #14679 (comment) with the example of \g<0>, which was deemed acceptably implausible. Because a replacement string can also include character escapes, this issue is plausible.

Alternatively, this could be considered a true positive, in which case the fix needs to interpret escape sequences in the replacement string.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghelp wantedContributions especially welcome

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions