Credence.Syntax.FixPythonFloorDiv (credence v0.7.1)

Copy Markdown

Replaces Python's // floor-division operator with Elixir's div/2.

LLMs translating from Python carry over the // operator for integer division. In Elixir, // is not a valid arithmetic operator (it only exists as the range step operator, first..last//step), so a // b does not parse. The integer-division function is div/2.

This is a Syntax rule because a // b won't parse in Elixir.

Detected patterns

a // b            n // 2
100 // 7          acc |> Kernel.//(k)
Kernel.//(a, b)

Any word // word where // is an infix operator between two identifiers or integers, plus the qualified Kernel.// call form.

Not flagged

Legitimate uses of // are not affected:

# // in comments              — comment lines are skipped
Enum.slice(list, 0..-2//1)     range step syntax `first..last//step`
Kernel./(a, b)                 single `/` is float division

Only word // word is rewritten. A // whose left operand is a parenthesised expression ((x + y) // 3) is left untouched — rewriting it safely needs a parser, which is unavailable for unparseable source.

Note on semantics

div/2 truncates toward zero, matching the way these LLM translations are used (the same convention as the sibling %rem/2 rule). It is not bit-identical to Python's floor // for negative operands; use Integer.floor_div/2 if exact Python floor semantics are required.

Bad

def half(n), do: n // 2
acc |> Kernel.//(k) |> do_step()

Good

def half(n), do: div(n, 2)
acc |> div(k) |> do_step()