Credence.Pattern.NoKeywordGetIntegerKey (credence v0.5.0)

Copy Markdown

Detects Keyword.get(list, integer) where the key is an integer literal.

Keyword.get/2 requires atom keys — its guard is when is_atom(key). Passing an integer key always crashes at runtime with FunctionClauseError.

LLMs produce this when translating Python's list[-1] (last element) or dict[index] into Elixir, reaching for Keyword.get as the closest-looking dictionary lookup.

Bad

Keyword.get(acc, -1)
Keyword.get(list, 0)
acc |> Keyword.get(-1)

Good

List.last(acc)
List.first(list)
acc |> List.last()

What is flagged

Any call to Keyword.get with exactly two arguments where the second is an integer literal (direct call), or one argument that is an integer literal (piped call). Three-argument calls are not flagged.

Auto-fix

Rewrites based on the index value:

Keyword.get(var, -1)    List.last(var)
Keyword.get(var, 0)     List.first(var)
Keyword.get(var, n)     Enum.at(var, n)

Only fixes when the list argument is a simple variable name.