Credence.Pattern.NoCondTwoClauses
(credence v0.7.1)
Copy Markdown
Detects cond with exactly two clauses where the second guard is
redundant — either literal true or the logical complement of the
first guard. Both patterns are just an if/else in disguise.
Bad
cond do
low > high -> false
true ->
mid = div(low + high, 2)
search(mid, target)
end
cond do
x <= target -> go_right
x > target -> found
endGood
if low > high do
false
else
mid = div(low + high, 2)
search(mid, target)
end
if x <= target do
go_right
else
found
endAuto-fix
Rewrites as if/else using the first clause's guard as the
condition. The condition is never modified.
For the complement case the rewrite is only applied when the guard's
operands are plain variables or literals. A cond's second guard is
re-evaluated when the first is false, whereas the if/else evaluates the
condition only once; restricting to side-effect-free, deterministic
operands keeps the fix behaviour-preserving on every input.