Credence.Pattern.PreferErlangFloat (credence v0.5.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.

Detected patterns

var * 1.0      1.0 * var
var / 1.0
var + 0.0      0.0 + var
var - 0.0

Note: 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.0

Good

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).