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 mode — var == -1 / var != -1 / === / !==
Ordering mode — var >= 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
endGood — equality
last_seen = Map.get(char_map, grapheme)
if last_seen != nil and last_seen >= start_index do
last_seen + 1
else
start_index
endBad — ordering
previous = Map.get(char_map, current_char, -1)
if previous >= left_index do
previous + 1
else
left_index
endGood — ordering
previous = Map.get(char_map, current_char)
if previous != nil and previous >= left_index do
previous + 1
else
left_index
endAuto-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 != nilguard.