Credence.Pattern.PreferNegateIfTrueFalse (credence v0.8.0)

Copy Markdown

Detects if cond do false else body end and rewrites it to if !cond do body else false end.

Why this matters

The pattern if cond do false else body end is non-idiomatic Elixir. When the do branch is just false, negate the condition, swap the branches, and keep the explicit false in the else branch to preserve the boolean return type.

Bad

if MapSet.member?(seen, current) do
  false
else
  MapSet.put(seen, current)
  |> loop(sum_of_squared_digits(current))
end

Good

if !MapSet.member?(seen, current) do
  MapSet.put(seen, current)
  |> loop(sum_of_squared_digits(current))
else
  false
end

Auto-fix

Negates the condition and swaps the branches: moves the else body to the do branch, and places an explicit false in the else branch.