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