contract_with_opaque.ex (1355B)
1 defmodule Dialyxir.Warnings.ContractWithOpaque do 2 @moduledoc """ 3 The @spec says the function is returning an opaque type, but it is 4 returning a different type. 5 6 ## Example 7 8 defmodule Types do 9 @opaque type :: :ok 10 end 11 12 defmodule Example do 13 @spec ok() :: Types.type() 14 def ok() do 15 :ok 16 end 17 end 18 """ 19 20 @behaviour Dialyxir.Warning 21 22 @impl Dialyxir.Warning 23 @spec warning() :: :contract_with_opaque 24 def warning(), do: :contract_with_opaque 25 26 @impl Dialyxir.Warning 27 @spec format_short([String.t()]) :: String.t() 28 def format_short([_module, function | _]) do 29 "The @spec for #{function} has an opaque subtype which is violated by the success typing." 30 end 31 32 @impl Dialyxir.Warning 33 @spec format_long([String.t()]) :: String.t() 34 def format_long([module, function, arity, type, signature_type]) do 35 pretty_module = Erlex.pretty_print(module) 36 pretty_type = Erlex.pretty_print_type(type) 37 pretty_success_type = Erlex.pretty_print_contract(signature_type) 38 39 """ 40 The @spec for #{pretty_module}.#{function}/#{arity} has an opaque 41 subtype #{pretty_type} which is violated by the success typing. 42 43 Success typing: 44 #{pretty_success_type} 45 """ 46 end 47 48 @impl Dialyxir.Warning 49 @spec explain() :: String.t() 50 def explain() do 51 @moduledoc 52 end 53 end