Skip to main content

W0032 - Maps Find Function Rather Than Syntax

Warning

fn(Key, Found, NotFound, Map) ->
case maps:find(Key,Map) of
{ok, V} -> Found;
error -> NotFound
end.
%% ^^^^^^^^^^^^^^^^^^^^^^^^^^^💡 warning: Unnecessary allocation of result tuple when the key is found.

Explanation

The warning message indicates that a map is being queried by key using the maps:find/2 function call rather than the dedicated lookup syntax.

Whilst this will correctly query for the value for the given key in the map, it is not considered idiomatic, and may be less efficient than using the map lookup syntax directly since it constructs an {ok,Value} tuple if they key is found.

To fix the issue, use the built-in #{Key := Value} pattern syntax to match against the Map:

fn(Key, Found, NotFound, Map) ->
case Map of
#{Key := Value} -> Found;
error -> NotFound
end.