Credence.Pattern.NoMapGetSentinel (credence v0.5.0)

Copy Markdown

Detects Map.get(map, key, -1) followed by a comparison against the sentinel — a Python dict.get(key, -1) idiom that leaks into LLM-generated Elixir.

Idiomatic Elixir uses nil as the absence marker (the default for Map.get/2) and checks with != nil or pattern-matches with Map.fetch/2.

Detection

Two modes:

Equality modevar == -1 / var != -1 / === / !== Ordering modevar >= expr / var > expr etc. where NEITHER operand is the sentinel literal and no equality check exists.

Only flags when:

  • The default is a negative integer literal (-1, -2, -999, etc.)
  • The result variable appears in a matching comparison
  • Both are in the same block with no rebinding between them

Bad — equality

last_seen = Map.get(char_map, grapheme, -1)
if last_seen != -1 and last_seen >= start_index do
  last_seen + 1
else
  start_index
end

Good — equality

last_seen = Map.get(char_map, grapheme)
if last_seen != nil and last_seen >= start_index do
  last_seen + 1
else
  start_index
end

Bad — ordering

previous = Map.get(char_map, current_char, -1)
if previous >= left_index do
  previous + 1
else
  left_index
end

Good — ordering

previous = Map.get(char_map, current_char)
if previous != nil and previous >= left_index do
  previous + 1
else
  left_index
end

Auto-fix

  • Equality: drops the sentinel default and replaces sentinel comparisons with nil.
  • Ordering: drops the sentinel default and wraps each ordering comparison with a var != nil guard.