callback_spec_argument_type_mismatch.ex (1585B)
1 defmodule Dialyxir.Warnings.CallbackSpecArgumentTypeMismatch do 2 @moduledoc """ 3 Spec type of argument does not match the callback's expected type. 4 5 ## Example 6 7 defmodule ExampleBehaviour do 8 @callback ok(:ok) :: :ok 9 end 10 11 defmodule Example do 12 @behaviour ExampleBehaviour 13 14 @spec ok(:error) :: :ok 15 def ok(:ok) do 16 :ok 17 end 18 end 19 """ 20 21 @behaviour Dialyxir.Warning 22 23 @impl Dialyxir.Warning 24 @spec warning() :: :callback_spec_arg_type_mismatch 25 def warning(), do: :callback_spec_arg_type_mismatch 26 27 @impl Dialyxir.Warning 28 @spec format_short([String.t()]) :: String.t() 29 def format_short([_behaviour, function | _]) do 30 "Spec type mismatch in argument to callback #{function}." 31 end 32 33 @impl Dialyxir.Warning 34 @spec format_long([String.t()]) :: String.t() 35 def format_long([behaviour, function, arity, position, success_type, callback_type]) do 36 pretty_behaviour = Erlex.pretty_print(behaviour) 37 pretty_success_type = Erlex.pretty_print_type(success_type) 38 pretty_callback_type = Erlex.pretty_print_type(callback_type) 39 ordinal_position = Dialyxir.WarningHelpers.ordinal(position) 40 41 """ 42 The @spec type for the #{ordinal_position} argument is not a 43 supertype of the expected type for the #{function}/#{arity} callback 44 in the #{pretty_behaviour} behaviour. 45 46 Success type: 47 #{pretty_success_type} 48 49 Behaviour callback type: 50 #{pretty_callback_type} 51 """ 52 end 53 54 @impl Dialyxir.Warning 55 @spec explain() :: String.t() 56 def explain() do 57 @moduledoc 58 end 59 end