Warn when invalid fun typespec is used
This commit is contained in:
@@ -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(
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user