elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.5k stars 3.38k forks source link

Wrong positive struct warning on rescue clause #13980

Closed dominicletz closed 3 hours ago

dominicletz commented 3 hours ago

Elixir and Erlang/OTP versions

$ elixir -v Erlang/OTP 27 [erts-15.1.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Elixir 1.17.3 (compiled with Erlang/OTP 27)

Operating system

Ubuntu

Current behavior

$ cat test.ex
defmodule Test do
    def maybe_unzip(term) do
        try do
          :zlib.unzip(term)
        rescue
          [ErlangError, :data_error] ->
            term
        end
    end
end

$ elixir test.ex 
warning: struct :data_error is undefined (module :data_error is not available or is yet to be defined)
└─ test.ex: Test.maybe_unzip/1

Expected behavior

$ cat test.ex
defmodule Test do
    def maybe_unzip(term) do
        try do
          :zlib.unzip(term)
        rescue
          [ErlangError, :data_error] ->
            term
        end
    end
end

$ elixir test.ex 
(no output)
josevalim commented 3 hours ago

The warning is correct. rescue only rescues exceptions, all Erlang errors are normalized into a single %ErlangError{} or you can use catch :error, :data_error.

josevalim commented 3 hours ago

To be clear, either do rescue _ -> term or rescue ErlangError -> _. The :data_error is not doing anything.

dominicletz commented 2 hours ago

Thanks for clarifying, I've no idea how I got to this construct above - but I did believe it would catch the specific :data_error subtype...