Elixir: Unused alias warning

Created on 23 Jul 2018  路  3Comments  路  Source: elixir-lang/elixir

Environment

  • Elixir: 1.6.6 / 1.8.0-dev

Current behavior

defmodule Foo do
  if Mix.env() == :prod do
    alias Foo.Bar
    def foo do
      Bar
    end
  end
end

mix compile in dev env produces the warning:

warning: unused alias Bar
 foo.ex:3

Expected behavior

No warning.

Elixir (compiler) Bug Unresolved

Most helpful comment

For anyone coming across this in the future this is the syntax you need to get rid of the warning:

defmodule Foo do
  if Mix.env() == :prod do
    alias Foo.Bar, warn: false
    def foo do
      Bar
    end
  end
end

All 3 comments

I don't think we can do anything about this. The body level, including the if block, is analyzed but the content of the function is not, because we never execute the def. You have to set warn: false in those cases.

For anyone coming across this in the future this is the syntax you need to get rid of the warning:

defmodule Foo do
  if Mix.env() == :prod do
    alias Foo.Bar, warn: false
    def foo do
      Bar
    end
  end
end

Just to be sure, is this the same thing here:

defmodule Foo do
  use Ecto.Schema

  alias Foo.Bar

  schema "foo" do
    embeds_one :bla, Bla do
      embeds_one(:bar, Bar)
    end
  end
end
Was this page helpful?
0 / 5 - 0 ratings