Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FURB156 false positive for multi-character string before in #13802

Closed
dscorbett opened this issue Oct 17, 2024 · 1 comment · Fixed by #14233
Closed

FURB156 false positive for multi-character string before in #13802

dscorbett opened this issue Oct 17, 2024 · 1 comment · Fixed by #14233
Assignees
Labels
bug Something isn't working preview Related to preview mode features

Comments

@dscorbett
Copy link

hardcoded-string-charset (FURB156) has a false positive with an incorrect fix. Given an expression of the form <expression> in <string literal>, the rule tries to replace the string literal with a variable from the string module, ignoring the order of the characters in the string literal. That is only valid when the left-hand-side expression is a string of length 1. If it is multiple characters, reordering the characters in the right-hand-side expression can change the value of the in operation.

$ ruff --version
ruff 0.7.0

$ cat furb156.py
print("89" in "9876543210")

$ python furb156.py
False

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

$ cat furb156.py
import string
print("89" in string.digits)

$ python furb156.py
True
@AlexWaygood AlexWaygood added the bug Something isn't working label Oct 17, 2024
@MichaReiser MichaReiser added the preview Related to preview mode features label Oct 18, 2024
@Aditya-PS-05
Copy link
Contributor

@MichaReiser ,
Please review it

@charliermarsh charliermarsh self-assigned this Nov 9, 2024
charliermarsh added a commit that referenced this issue Nov 9, 2024
…sets (#14233)

## Summary

It's only safe to enforce the `x in "1234567890"` case if `x` is exactly
one character, since the set on the right has been reordered as compared
to `string.digits`. We can't know if `x` is exactly one character unless
it's a literal. And if it's a literal, well, it's kind of silly code in
the first place?

Closes #13802.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working preview Related to preview mode features
Projects
None yet
5 participants