Credence.Pattern.NoIfTrueFalse
(credence v0.7.0)
Copy Markdown
Detects redundant if/else wrappers around boolean expressions.
LLMs frequently emit this redundant boolean wrapper when the condition
already evaluates to a boolean. An if whose do branch returns true
and whose else branch returns false (or a wildcard default) is
semantically identical to the condition itself.
Also catches the generalised forms where both branches are boolean expressions or literals.
Detected patterns
if condition do true else false end → condition
if condition do bool_expr else false end → condition and bool_expr
if condition do false else true end → not condition
if condition do false else bool_expr end → not condition and bool_expr
if condition do true else bool_expr end → condition or bool_expr
if condition do bool_expr else true end → not condition or bool_exprBad
if match?([_, _, _, _], parts) and Enum.all?(parts, &valid?/1) do
true
else
false
end
if x > 0 do
y == 1
else
false
end
if rem(year, 100) == 0 do
rem(year, 400) == 0
else
true
endGood
match?([_, _, _, _], parts) and Enum.all?(parts, &valid?/1)
x > 0 and y == 1
rem(year, 100) != 0 or rem(year, 400) == 0Auto-fix
if cond do true else false end→condif cond do expr else false end→cond and exprif cond do false else true end→not condif cond do false else expr end→not cond and exprif cond do true else expr end→cond or exprif cond do expr else true end→not cond or expr