missing_range.ex (1504B)
1 defmodule Dialyxir.Warnings.MissingRange do 2 @moduledoc """ 3 Function spec declares a list of types, but function returns value 4 outside stated range. 5 6 This error only appears with the :overspecs flag. 7 8 ## Example 9 10 defmodule Example do 11 @spec foo(any()) :: :ok 12 def foo(:ok) do 13 :ok 14 end 15 16 def foo(_) do 17 :error 18 end 19 end 20 """ 21 22 @behaviour Dialyxir.Warning 23 24 @impl Dialyxir.Warning 25 @spec warning() :: :missing_range 26 def warning(), do: :missing_range 27 28 @impl Dialyxir.Warning 29 @spec format_short([String.t()]) :: String.t() 30 def format_short([module, function, arity | _]) do 31 pretty_module = Erlex.pretty_print(module) 32 33 "The type specification is missing types returned by #{pretty_module}.#{function}/#{arity}." 34 end 35 36 @impl Dialyxir.Warning 37 @spec format_long([String.t()]) :: String.t() 38 def format_long([module, function, arity, extra_ranges, contract_range]) do 39 pretty_module = Erlex.pretty_print(module) 40 pretty_contract_range = Erlex.pretty_print_type(contract_range) 41 pretty_extra_ranges = Erlex.pretty_print_type(extra_ranges) 42 43 """ 44 The type specification is missing types returned by function. 45 46 Function: 47 #{pretty_module}.#{function}/#{arity} 48 49 Type specification return types: 50 #{pretty_contract_range} 51 52 Missing from spec: 53 #{pretty_extra_ranges} 54 """ 55 end 56 57 @impl Dialyxir.Warning 58 @spec explain() :: String.t() 59 def explain() do 60 @moduledoc 61 end 62 end