summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThiago Santos <thia.mdossantos@gmail.com>2021-12-22 18:01:14 -0300
committerGitHub <noreply@github.com>2021-12-22 22:01:14 +0100
commitaad9d6c0691e67db1ffbd00757bc65d6c0119fb6 (patch)
tree281b0be9ef9ce16c810052da339ab1d448163ea1
parentc384f19481ade59a651f3ef85c86656db4cf73e2 (diff)
downloadelixir-aad9d6c0691e67db1ffbd00757bc65d6c0119fb6.tar.gz
Warn on zero arity callbacks inside protocols (#11519)
-rw-r--r--lib/elixir/lib/protocol.ex4
-rw-r--r--lib/elixir/test/elixir/kernel/warning_test.exs8
2 files changed, 10 insertions, 2 deletions
diff --git a/lib/elixir/lib/protocol.ex b/lib/elixir/lib/protocol.ex
index 7bbcd7fd7..3c65c2af0 100644
--- a/lib/elixir/lib/protocol.ex
+++ b/lib/elixir/lib/protocol.ex
@@ -731,14 +731,14 @@ defmodule Protocol do
defp callback_ast_to_fa({kind, {:"::", meta, [{name, _, args}, _return]}, _pos})
when kind in [:callback, :macrocallback] do
- [{{name, length(args)}, meta}]
+ [{{name, length(List.wrap(args))}, meta}]
end
defp callback_ast_to_fa(
{kind, {:when, _, [{:"::", meta, [{name, _, args}, _return]}, _vars]}, _pos}
)
when kind in [:callback, :macrocallback] do
- [{{name, length(args)}, meta}]
+ [{{name, length(List.wrap(args))}, meta}]
end
defp callback_ast_to_fa({kind, _, _pos}) when kind in [:callback, :macrocallback] do
diff --git a/lib/elixir/test/elixir/kernel/warning_test.exs b/lib/elixir/test/elixir/kernel/warning_test.exs
index f25438439..6c780a35c 100644
--- a/lib/elixir/test/elixir/kernel/warning_test.exs
+++ b/lib/elixir/test/elixir/kernel/warning_test.exs
@@ -1168,9 +1168,11 @@ defmodule Kernel.WarningTest do
def without_specs(term, options \\ [])
+ @callback foo :: {:ok, term}
@callback foo(term) :: {:ok, term}
@callback foo(term, keyword) :: {:ok, term, keyword}
+ @callback foo_when :: {:ok, x} when x: term
@callback foo_when(x) :: {:ok, x} when x: term
@callback foo_when(x, opts) :: {:ok, x, opts} when x: term, opts: keyword
@@ -1184,12 +1186,18 @@ defmodule Kernel.WarningTest do
end)
assert message =~
+ "cannot define @callback foo/0 inside protocol, use def/1 to outline your protocol definition\n nofile:1"
+
+ assert message =~
"cannot define @callback foo/1 inside protocol, use def/1 to outline your protocol definition\n nofile:1"
assert message =~
"cannot define @callback foo/2 inside protocol, use def/1 to outline your protocol definition\n nofile:1"
assert message =~
+ "cannot define @callback foo_when/0 inside protocol, use def/1 to outline your protocol definition\n nofile:1"
+
+ assert message =~
"cannot define @callback foo_when/1 inside protocol, use def/1 to outline your protocol definition\n nofile:1"
assert message =~