False Positive warning about missing key in map
Closed this issue · 2 comments
Elixir and Erlang/OTP versions
Erlang/OTP 27 [erts-15.2.3] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]
Elixir 1.18.3 (compiled with Erlang/OTP 27)
Also reproducable with 1.18.4 and 1.19.0-rc.0 (OTP 27)
Operating system
MacOS 15.5
Current behavior
Trying to compile this module creates a compile-time warning
defmodule FalsePositiveTypeWarning do
def test_val() do
data = %{
list: [
%{
one: %{}
},
%{
two: %{
three: 1
}
}
]
}
[_a, b] = data.list
c = b.two
c.three
end
endwarning: unknown key .two in expression:
b.two
the given type does not have the given key:
%{one: empty_map()} or %{two: %{three: integer()}}
where "b" was given the type:
# type: %{one: empty_map()} or %{two: %{three: integer()}}
# from: iex:16
[_a, b] = data.list
└─ iex:17: FalsePositiveTypeWarning.test_val/0
Expected behavior
The given code should compile without warning
This warning, in particular, won't be addressed and it is one of the "false positives" that will become part of our workflows as we move forward. This is because lists are always treated as heterogeneous entities and not tracked by position. You can use tuples or maps, which are data types designed for that which allow you precise matching. Thanks for the report!
Understandable. Thankfully this only happened in a data-structure I wrote for a test. I'll try to work around it