Skip to content

FURB188 calculates lengths wrong for non-ASCII affixes #13620

@dscorbett

Description

@dscorbett

slice-to-remove-prefix-or-suffix (FURB188) determines string length by UTF-8 code units, as in Rust, whereas Python counts code points. This discrepancy causes problems for non-ASCII affixes. In the following, furb188.py has two parts: the first has a false positive and the second has a false negative.

$ ruff --version
ruff 0.6.8
$ cat furb188.py
text = "řetězec"
if text.startswith("ř"):
    text = text[2:]
print(text)

text = "řetězec"
if text.startswith("ř"):
    text = text[1:]
print(text)
$ python furb188.py
tězec
etězec
$ ruff check --isolated --preview --target-version py39 --select FURB188 furb188.py --fix
Found 1 error (1 fixed, 0 remaining).
$ cat furb188.py
text = "řetězec"
text = text.removeprefix("ř")
print(text)

text = "řetězec"
if text.startswith("ř"):
    text = text[1:]
print(text)
$ python furb188.py
etězec
etězec

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions