Skip to content

Warn on unused shadowed aliases #13550

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

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/elixir/lib/kernel/lexical_tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ defmodule Kernel.LexicalTracker do

@doc false
def handle_call(:unused_aliases, _from, state) do
{:reply, Enum.sort(state.aliases), state}
unused_aliases =
Enum.map(state.aliases, fn
{{:shadowed, module}, meta} -> {module, meta}
{module, meta} when is_atom(module) -> {module, meta}
end)
|> Enum.sort()

{:reply, unused_aliases, state}
end

def handle_call(:unused_imports, _from, state) do
Expand Down Expand Up @@ -220,6 +227,12 @@ defmodule Kernel.LexicalTracker do

def handle_cast({:add_alias, module, meta, warn}, state) do
if warn do
state =
case state do
%{aliases: %{^module => meta}} -> put_in(state.aliases[{:shadowed, module}], meta)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I reused the aliases map since it's only accessed within this file and it is a rare edge-case anyway, but could also have a shadowed_aliases key in the global state.

_ -> state
end

{:noreply, put_in(state.aliases[module], meta)}
else
{:noreply, state}
Expand Down
4 changes: 2 additions & 2 deletions lib/elixir/lib/macro/env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ defmodule Macro.Env do

## Examples

iex> alias List, as: MyList
iex> alias List, as: MyList, warn: false
iex> Macro.Env.expand_alias(__ENV__, [], [:MyList])
{:alias, List}
iex> Macro.Env.expand_alias(__ENV__, [], [:MyList, :Nested])
Expand All @@ -456,7 +456,7 @@ defmodule Macro.Env do
If there is no alias or the alias starts with `Elixir.`
(which disables aliasing), then `:error` is returned:

iex> alias List, as: MyList
iex> alias List, as: MyList, warn: false
iex> Macro.Env.expand_alias(__ENV__, [], [:Elixir, MyList])
:error
iex> Macro.Env.expand_alias(__ENV__, [], [:AnotherList])
Expand Down
6 changes: 6 additions & 0 deletions lib/elixir/test/elixir/kernel/alias_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ defmodule Kernel.AliasTest do
assert Billing == :"Elixir.MyApp.Billing"
end

test "overriding parent alias with child alias" do
alias MyApp.Billing
alias Billing.Billing
assert Billing == :"Elixir.MyApp.Billing.Billing"
end

test "lexical" do
if true_fun() do
alias OMG, as: List, warn: false
Expand Down
16 changes: 16 additions & 0 deletions lib/elixir/test/elixir/kernel/warning_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,22 @@ defmodule Kernel.WarningTest do
purge(Sample)
end

test "unused alias due to shadowing" do
assert_warn_compile(
["nofile:2:3", "unused alias Baz"],
"""
defmodule Sample do
alias Foo.Baz
alias Bar.Baz

def baz, do: Baz
end
"""
)
after
purge(Sample)
end

test "unused inside dynamic module" do
import List, only: [flatten: 1], warn: false

Expand Down