The length of a value is compared with zero where its truth value says the same
What it finds
if len(items) == 0: and if len(items) > 0: test emptiness, which if not items: and if items: do directly for every builtin container. For arrays and data frames, whose truth value is ambiguous, the length test is right, so the rule is in the strict tier. Reported are comparisons of a call of the builtin len with 0 using ==, != or >, in conditions of if, elif, while and conditional expressions.
From the rule's source, length_test_against_zero.rs.
def check(items): if len(items) == 0:tx0 reports: this tests emptiness through the length; not x says the same return 1 while len(items) > 0:tx0 reports: x says the same items.pop() count = len(items) == 0 return 2 if len(items) != 0 else 3tx0 reports: this tests emptiness through the length Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:length-test-against-zero" = "note"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:length-test-against-zero] reason="..."tx0 explain tx0/python:length-test-against-zero shows the same in the terminal. Found a false positive? Report it.