Warn when invalid fun typespec is used

This commit is contained in:
José Valim
2025-06-11 12:08:45 +02:00
parent 7da4d6570b
commit e04444c42e
2 changed files with 23 additions and 1 deletions
+9 -1
View File
@@ -877,7 +877,15 @@ defmodule Kernel.Typespec do
defp typespec({:fun, meta, args}, vars, caller, state) do
{args, state} = :lists.mapfoldl(&typespec(&1, vars, caller, &2), state, args)
{{:type, location(meta), :fun, args}, state}
if args != [] do
IO.warn(
"fun/#{length(args)} is not valid in typespecs. Either specify fun() or use (... -> return) instead",
caller
)
end
{{:type, location(meta), :fun, []}, state}
end
defp typespec({:..., _meta, _args}, _vars, caller, _state) do
@@ -1818,6 +1818,20 @@ defmodule Kernel.WarningTest do
purge([Sample1, Sample2, Sample3])
end
test "invalid fun" do
assert_warn_eval(
[
"nofile:2: ",
"fun/1 is not valid in typespecs. Either specify fun() or use (... -> return) instead"
],
"""
defmodule InvalidFunType do
@type my_type :: fun(integer())
end
"""
)
end
test "invalid type annotations" do
assert_warn_eval(
[