Skip to content

ExUnit: Unused alias warning with behaviour and nested modules #14419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
novaugust opened this issue Apr 9, 2025 · 2 comments
Closed

ExUnit: Unused alias warning with behaviour and nested modules #14419

novaugust opened this issue Apr 9, 2025 · 2 comments

Comments

@novaugust
Copy link
Contributor

Elixir and Erlang/OTP versions

Erlang/OTP 27 [erts-15.2.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1]

Elixir 1.18.2 (compiled with Erlang/OTP 27)

Operating system

macos

Current behavior

this this minimal repro emits an unused alias warning for alias A.B.C

defmodule A.B.C do
  @callback foo() :: :ok
end

defmodule A do
  use ExUnit.Case
  alias A.B.C

  test "with submodule" do
    defmodule B do
      @behaviour C
      @impl C
      def foo, do: :ok
    end

    assert true
  end
end
    warning: unused alias C
    │
  7 │   alias A.B.C
    │   ~
    │
    └─ test/foo_test.exs:7:3

Expected behavior

no warning.
obviously, there are trivial fixes the user can take to fix this situation, like

  1. define the submodules outside of the test macro
  2. move the alias into the submodule itself

but i thought it might be worth shining a light on for you. no worries if it's a wontfix =)
found via styler user issue

@josevalim
Copy link
Member

The contents of a defmodule are only executed when the code runs, in this case, the test. So it is a won't fix because the alias has indeed not been used by the time the file has done compiling. It is an expected false warning. You can pass alias A.B.C, warn: false instead. Thanks for the report!

@josevalim josevalim closed this as not planned Won't fix, can't repro, duplicate, stale Apr 9, 2025
@novaugust
Copy link
Contributor Author

thanks for the learnings =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants