Credence.Pattern.NoIdentityFloatCoercion (credence v0.6.0)

Copy Markdown

Detects identity arithmetic used to coerce an integer to a float.

LLMs carry Python idioms (x * 1.0, x / 1.0, x + 0.0) into Elixir, where they are unnecessary. If a float result is needed, Elixir's / operator always returns a float naturally.

Detected patterns

expr * 1.0      1.0 * expr
expr / 1.0
expr + 0.0      0.0 + expr
expr - 0.0

Note: 0.0 - expr is NOT flagged — it negates, not coerces.

Bare-variable skip

When the non-identity operand is a bare variable (n * 1.0), the rule skips it — the variable's type is unknown, and * 1.0 may be intentional int → float coercion. Compound expressions ((a + b) * 1.0), function calls (Enum.at(list, 0) * 1.0), and literals are still flagged because the * 1.0 is overwhelmingly a Python-ism in those contexts.

The companion rule PreferErlangFloat handles the bare-variable cases by rewriting n * 1.0 to :erlang.float(n).

Bad

Enum.at(sorted_list, mid) * 1.0

Enum.at(combined, mid_index) / 1.0

Good

Enum.at(sorted_list, mid)

Enum.at(combined, mid_index)

Auto-fix

Removes the identity operand and operator from the expression. When the entire line is a no-op self-assignment (var = var * 1.0), the line is deleted.