[Proposal] Warn on duplicated aliases

23 views
Skip to first unread message

Vinícius Müller

unread,
Sep 11, 2023, 8:40:48 PM9/11/23
to elixir-lang-core
Would it be possible for us to warn when an user declares duplicated aliases in the same scope?

This code, for example:

```
defmodule Test do
  alias IO.ANSI
  alias IO.ANSI
  alias IO.ANSI

  def hello do
    IO.puts([ANSI.red(), "hey", ANSI.reset()])
  end
end
```

Compiles without any warnings, even though two of these aliases are just "dead code".

I've noticed this after seeing some codebases containing files with these duplicated aliases, something that can actually happening sometimes when people solve conflicts and don't review it thoroughly.

Also credo currently does not catch that and I believe a proposal there would also be fine, but I'm not sure why the language couldn't warn in such cases, since this is more like dead code than styling/linting.

Christopher Keele

unread,
Sep 12, 2023, 2:31:28 PM9/12/23
to elixir-lang-core
We'd need to go deeper with the logic here than just checking for literally "duplicated" alias calls, as aliases can impact later ones—for a contrived example, see:

```
defmodule Na.Na.Na.Na.Na.Na.Batman do
  def batman!, do: :batman
end

defmodule Test do
  alias Na.Na
  alias Na.Na
  alias Na.Na
  alias Na.Na
  import Na.Batman
  
  batman!()
  batman!()
  batman!()
end
```
Reply all
Reply to author
Forward
0 new messages