- Language: Python
- Rule: python:S6659
- The suggested improvement would not yield the same comparison results
- Using SonarQube for IDE v4.23.0 in VS Code v1.100.2, not in connected mode
- Following code will produce the rule suggestion and also demonstrate a scenario where the suggested improvement would fail to achieve the same resutls:
foo = "foo"
foobar = "foobar"
foobarfoo = "foobarfoo"
print("With splicing:")
print("do not want foo to match, and it does not")
print(foobarfoo[:-3] == foo)
print("want foobar to match, and it does")
print(foobarfoo[:-3] == foobar)
print()
print("With startswith")
print("do not want foo to match, but it does")
print(foobarfoo.startswith(foo))
print("want foobar to match, and it does")
print(foobarfoo.startswith(foobar))
Hello!
Thank you for reporting this case.
I can confirm that the behavior is different in this case because the functions look at more than just the sliced range.
The rule description should be expanded to have information on this situation, I have created SONARPY-3024 to track the change.
Have a good day,