unmatched_return.ex (1594B)
1 defmodule Dialyxir.Warnings.UnmatchedReturn do 2 @moduledoc """ 3 The invoked expression returns a union of types and the call does 4 not match on its return types using e.g. a case or wildcard. 5 6 ## Example 7 8 defmodule Example do 9 require Integer 10 11 def ok() do 12 n = :rand.uniform(100) 13 14 multiple_returns(n) 15 16 :ok 17 end 18 19 defp multiple_returns(n) do 20 if Integer.is_even(n) do 21 :ok 22 else 23 {:error, "error"} 24 end 25 end 26 end 27 28 This would NOT result in a warning: 29 30 defmodule Example do 31 require Integer 32 33 def ok() do 34 n = :rand.uniform(100) 35 36 multiple_returns(n) 37 38 :ok 39 end 40 41 defp multiple_returns(n) do 42 if Integer.is_even(n) do 43 :ok 44 else 45 :error 46 end 47 end 48 end 49 """ 50 @behaviour Dialyxir.Warning 51 52 @impl Dialyxir.Warning 53 @spec warning() :: :unmatched_return 54 def warning(), do: :unmatched_return 55 56 @impl Dialyxir.Warning 57 @spec format_short([String.t()]) :: String.t() 58 def format_short(_) do 59 "The expression produces multiple types, but none are matched." 60 end 61 62 @impl Dialyxir.Warning 63 @spec format_long([String.t()]) :: String.t() 64 def format_long([type]) do 65 pretty_type = Erlex.pretty_print_type(type) 66 67 """ 68 The expression produces a value of type: 69 70 #{pretty_type} 71 72 but this value is unmatched. 73 """ 74 end 75 76 @impl Dialyxir.Warning 77 @spec explain() :: String.t() 78 def explain() do 79 @moduledoc 80 end 81 end