negative_guard_fail.ex (1316B)
1 defmodule Dialyxir.Warnings.NegativeGuardFail do 2 @moduledoc """ 3 The function guard either presents an impossible guard or the only 4 calls will never succeed against the guards. 5 6 ## Example 7 8 defmodule Example do 9 def ok(ok = "ok") when not is_bitstring(ok) do 10 :ok 11 end 12 end 13 14 or 15 16 defmodule Example do 17 def ok() do 18 ok(:ok) 19 end 20 21 defp ok(ok) when not is_atom(ok) do 22 :ok 23 end 24 end 25 """ 26 27 @behaviour Dialyxir.Warning 28 29 @impl Dialyxir.Warning 30 @spec warning() :: :neg_guard_fail 31 def warning(), do: :neg_guard_fail 32 33 @impl Dialyxir.Warning 34 @spec format_short([String.t()]) :: String.t() 35 def format_short(_) do 36 "The guard test can never succeed." 37 end 38 39 @impl Dialyxir.Warning 40 @spec format_long([String.t()]) :: String.t() 41 def format_long([guard, args]) do 42 pretty_args = Erlex.pretty_print_args(args) 43 44 """ 45 Guard test: 46 not #{guard}#{pretty_args} 47 48 can never succeed. 49 """ 50 end 51 52 def format_long([arg1, infix, arg2]) do 53 pretty_infix = Erlex.pretty_print_infix(infix) 54 55 """ 56 Guard test: 57 not #{arg1} #{pretty_infix} #{arg2} 58 59 can never succeed. 60 """ 61 end 62 63 @impl Dialyxir.Warning 64 @spec explain() :: String.t() 65 def explain() do 66 @moduledoc 67 end 68 end