-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
guard_fail.ex
76 lines (58 loc) · 1.42 KB
/
guard_fail.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
defmodule Dialyxir.Warnings.GuardFail do
@moduledoc """
The function guard either presents an impossible guard or the only
calls will never succeed against the guards.
## Example
defmodule Example do
def ok() do
ok(0)
end
defp ok(n) when n > 1 do
:ok
end
end
or
defmodule Example do
def ok() when 0 > 1 do
:ok
end
end
"""
@behaviour Dialyxir.Warning
@impl Dialyxir.Warning
@spec warning() :: :guard_fail
def warning(), do: :guard_fail
@impl Dialyxir.Warning
@spec format_short([String.t()]) :: String.t()
def format_short(_) do
"The guard clause can never succeed."
end
@impl Dialyxir.Warning
@spec format_long([String.t()]) :: String.t()
def format_long([]) do
"The guard clause can never succeed."
end
def format_long([guard, args]) do
pretty_args = Erlex.pretty_print_args(args)
"""
The guard test:
#{guard}#{pretty_args}
can never succeed.
"""
end
def format_long([arg, infix, guard]) do
pretty_arg = Erlex.pretty_print_args(arg)
pretty_infix = Erlex.pretty_print_infix(infix)
pretty_guard = Erlex.pretty_print(guard)
"""
The guard clause:
when #{pretty_arg} #{pretty_infix} #{pretty_guard}
can never succeed.
"""
end
@impl Dialyxir.Warning
@spec explain() :: String.t()
def explain() do
@moduledoc
end
end