/
invalid_contract.ex
54 lines (43 loc) · 1.3 KB
/
invalid_contract.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
defmodule Dialyxir.Warnings.InvalidContract do
@moduledoc """
The @spec for the function does not match the success typing of the
function.
## Example
defmodule Example do
@spec process(:error) :: :ok
def process(:ok) do
:ok
end
end
The @spec in this case claims that the function accepts a parameter
:error but the function head only accepts :ok, resulting in the
mismatch.
"""
@behaviour Dialyxir.Warning
@impl Dialyxir.Warning
@spec warning() :: :invalid_contract
def warning(), do: :invalid_contract
@impl Dialyxir.Warning
@spec format_short([String.t()]) :: String.t()
def format_short([_module, function | _]) do
"Invalid type specification for function #{function}."
end
@impl Dialyxir.Warning
@spec format_long([String.t()]) :: String.t()
def format_long([module, function, arity, signature]) do
pretty_module = Erlex.pretty_print(module)
pretty_signature = Erlex.pretty_print_contract(signature)
"""
The @spec for the function does not match the success typing of the function.
Function:
#{pretty_module}.#{function}/#{arity}
Success typing:
@spec #{function}#{pretty_signature}
"""
end
@impl Dialyxir.Warning
@spec explain() :: String.t()
def explain() do
@moduledoc
end
end