Credence.Pattern.NoTautologicalIf (credence v0.7.0)

Copy Markdown

Detects if/else expressions where both branches return the same value.

When both the do and else branches produce identical code, the condition is irrelevant — the entire if/else can be replaced with the body of either branch.

Detected patterns

if condition do
  result
else
  result
end

if swapped do
  result
else
  result
end

if condition, do: value, else: value

Good

result

value

Auto-fix

Replaces the entire if/else with the do branch body.

Safety: why this is narrowed

Deleting the if also deletes the condition, so the condition must be provably free of side effects and exceptions — otherwise dropping it changes behaviour (a side-effecting or raising condition would no longer run). We only fire when the condition is a pure, total expression: a variable, a literal, or a comparison of such. Any function call (foo(), f(x)) or assignment (x = ...) in the condition is excluded.

Lifting the branch body out of the if scope also makes any variable the body binds leak to the surrounding scope, which can shadow an outer variable used later. We therefore only fire when the body contains no match (=) — bindings inside fn/case/for/etc. are scoped and harmless, but a bare = could leak, so we exclude the whole body if any = appears.