callback_type_mismatch.ex (1347B)
1 defmodule Dialyxir.Warnings.CallbackTypeMismatch do 2 @moduledoc """ 3 The success type of the function does not match the callback type in 4 the behaviour. 5 6 ## Example 7 8 defmodule ExampleBehaviour do 9 @callback ok() :: :ok 10 end 11 12 defmodule Example do 13 @behaviour ExampleBehaviour 14 15 def ok() do 16 :error 17 end 18 end 19 """ 20 21 @behaviour Dialyxir.Warning 22 23 @impl Dialyxir.Warning 24 @spec warning() :: :callback_type_mismatch 25 def warning(), do: :callback_type_mismatch 26 27 @impl Dialyxir.Warning 28 @spec format_short([String.t()]) :: String.t() 29 def format_short([_behaviour, function | _]) do 30 "Type mismatch for @callback #{function}." 31 end 32 33 @impl Dialyxir.Warning 34 @spec format_long([String.t() | non_neg_integer]) :: String.t() 35 def format_long([behaviour, function, arity, fail_type, success_type]) do 36 pretty_behaviour = Erlex.pretty_print(behaviour) 37 pretty_fail_type = Erlex.pretty_print_type(fail_type) 38 pretty_success_type = Erlex.pretty_print_type(success_type) 39 40 """ 41 Type mismatch for @callback #{function}/#{arity} in #{pretty_behaviour} behaviour. 42 43 Expected type: 44 #{pretty_success_type} 45 46 Actual type: 47 #{pretty_fail_type} 48 """ 49 end 50 51 @impl Dialyxir.Warning 52 @spec explain() :: String.t() 53 def explain() do 54 @moduledoc 55 end 56 end