Credence.Pattern.NoDoubleFilter
(credence v0.7.0)
Copy Markdown
Detects two adjacent Enum.filter/2 calls on the same variable
whose predicates are exact logical complements, which can be replaced
with a single Enum.split_with/2 pass.
Bad
non_neg = Enum.filter(numbers, &(&1 >= 0))
neg = Enum.filter(numbers, &(&1 < 0))Good
{non_neg, neg} = Enum.split_with(numbers, &(&1 >= 0))Why so narrow
Enum.split_with/2 derives the second list as the elements that fail
the single predicate — so the rewrite is only behaviour-preserving when
the second filter's predicate is the exact complement of the first's.
Determining that for arbitrary predicates is undecidable, so this rule
fires only on the shape it can prove: both predicates are captures
&(&1 <op> <operand>) over the same simple <operand> (a literal or a
bare variable, never a call — to rule out side-effecting double
evaluation), with <op> pairs that partition every term under Elixir's
total ordering:
>=/<>/<===/!=
The two assignments must be adjacent (no statement between them) and bind distinct variables.