A value is removed from a set after testing that it is there, which discard does in one call

tx0/python:check-before-removeallnotestyle/conventionsdepth faststyle

What it finds

if x in s: s.remove(x) looks the value up twice; s.discard(x) removes it if present and does nothing otherwise. Lists have no discard, so this applies when the receiver is known to be a set from its display or set() call in the same function.

From the rule's source, check_before_remove.rs.

Example from the rule's tests
def drop(x, items):    seen = set()    if x in seen:tx0 reports: discard removes the value if it is there, in one lookup        seen.remove(x)    if x in items:        items.remove(x)    return seen
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the all tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:check-before-remove" = "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:check-before-remove] reason="..."

tx0 explain tx0/python:check-before-remove shows the same in the terminal. Found a false positive? Report it.