Credence.Pattern.PreferErlangFloat
(credence v0.6.0)
Copy Markdown
Replaces bare-variable float coercion tricks with explicit :erlang.float/1.
LLMs (and developers) use n * 1.0, n / 1.0, n + 0.0, or n - 0.0
to coerce an integer to a float. These are arithmetic tricks borrowed from
Python — :erlang.float/1 expresses the same intent explicitly and works
whether the input is an integer (converts) or already a float (returns it).
This rule only handles bare-variable operands. Compound expressions
and function calls ((a + b) * 1.0, Enum.sum(list) * 1.0) are handled
by NoIdentityFloatCoercion, which removes the identity outright — those
are overwhelmingly Python-isms, not intentional coercion.
Priority
This rule runs at priority 501 (above the default 500) so it processes
bare-variable sites before NoIdentityFloatCoercion. This matters
when both kinds share a line — e.g. {n * 1.0, Enum.sum(xs) * 1.0}.
Without the higher priority, NoIdentityFloatCoercion's line-level regex
would strip all * 1.0 on the line, including the bare-variable site
that should become :erlang.float(n).
Detected patterns
var * 1.0 1.0 * var
var / 1.0
var + 0.0 0.0 + var
var - 0.0Note: 0.0 - var is NOT flagged — it negates, not coerces.
Bad
defp to_float(n) when is_integer(n), do: n * 1.0
count = count + 0.0Good
defp to_float(n) when is_integer(n), do: :erlang.float(n)
count = :erlang.float(count)Auto-fix
Replaces the identity arithmetic with :erlang.float(var).