Skip to content

Compilation warning when using super with unused, overridable function via using module #14094

Closed
@acco

Description

@acco

Elixir and Erlang/OTP versions

Elixir 1.18.0 (compiled with Erlang/OTP 27)

Operating system

MacOS Sequoia 15.1.1 (24B91)

Current behavior

Thanks for a great release!

When using a library with super, this implementation:

defmodule Sequin.Encrypted.Field do
  use Cloak.Ecto.Binary, vault: Sequin.Vault

  def load(nil), do: {:ok, nil}

  def load(value) do
    super(Base.decode64!(value))
  end
end

Results in a compilation error:

  warning: this clause of defp load (overridable 1)/1 is never used
    │
  8 │   use Cloak.Ecto.Binary, vault: Sequin.Vault
    │   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ lib/sequin/encrypted/field.ex:8: Sequin.Encrypted.Field."load (overridable 1)"/1

The compilation error goes away if I call super/1 in the first load/1:

defmodule Sequin.Encrypted.Field do
  use Cloak.Ecto.Binary, vault: Sequin.Vault

  def load(nil), do: super(nil)

  def load(value) do
    super(Base.decode64!(value))
  end
end

The library defines two overridable load/1 functions:

 defmacro __using__(opts) do
    vault = Keyword.fetch!(opts, :vault)
    label = opts[:label]
    closure = !!opts[:closure]

    quote location: :keep do
    
    
      @doc false
      @impl Ecto.Type
      def load(nil) do
        {:ok, nil}
      end

      def load(value) do
        with {:ok, value} <- decrypt(value) do
          value = after_decrypt(value)

          if unquote(closure) do
            {:ok, fn -> value end}
          else
            {:ok, value}
          end
        else
          _other ->
            :error
        end
      end

So, it appears I need to use both overridable functions in order to not get a compilation warning.

Expected behavior

Because I don't "own" the module I'm using, I'd expect the compiler to not care that one of the clauses is unused?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions