Skip to content

Commit 5e5eaca

Browse files
ggcampinhoJosé Valim
authored andcommitted
Warn duplicate definitions in macros (#7037)
Closes #6637
1 parent ab61c09 commit 5e5eaca

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/elixir/src/elixir_def.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ store_definition(Kind, CheckClauses, Call, Body, Pos) ->
9696
%% extract meta information like file and context.
9797
{_, Meta, _} = Call,
9898
DoCheckClauses = (not lists:keymember(context, 1, Meta)) andalso (CheckClauses),
99+
Generated = case lists:keyfind(generated, 1, Meta) of
100+
{generated, true} -> ?generated([]);
101+
_ -> []
102+
end,
99103

100104
%% Check if there is a file information in the definition.
101105
%% If so, we assume this come from another source and
@@ -114,7 +118,6 @@ store_definition(Kind, CheckClauses, Call, Body, Pos) ->
114118
LinifyArgs = elixir_quote:linify(Line, Key, Args),
115119
LinifyGuards = elixir_quote:linify(Line, Key, Guards),
116120
LinifyBody = elixir_quote:linify(Line, Key, Body),
117-
Generated = case DoCheckClauses of true -> []; false -> ?generated([]) end,
118121

119122
{EL, MetaLocation} =
120123
case retrieve_location(Location, ?key(E, module)) of

lib/elixir/test/elixir/kernel/warning_test.exs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,27 @@ defmodule Kernel.WarningTest do
487487
purge Sample
488488
end
489489

490+
test "generated clause not match" do
491+
assert capture_err(fn ->
492+
Code.eval_string("""
493+
defmodule Sample do
494+
defmacro __using__(_) do
495+
quote do
496+
def hello, do: nil
497+
def hello, do: nil
498+
end
499+
end
500+
end
501+
defmodule UseSample do
502+
use Sample
503+
end
504+
""")
505+
end) =~ "this clause cannot match because a previous clause at line 10 always matches"
506+
after
507+
purge(Sample)
508+
purge(UseSample)
509+
end
510+
490511
test "clause with defaults should be first" do
491512
assert capture_err(fn ->
492513
Code.eval_string ~S"""

0 commit comments

Comments
 (0)