pattern_match_covered.ex (1236B)
1 defmodule Dialyxir.Warnings.PatternMatchCovered do 2 @moduledoc """ 3 The pattern match has a later clause that will never be executed, 4 because a more general clause is higher in the matching order. 5 6 ## Example 7 8 defmodule Example do 9 def ok() do 10 unmatched(:error) 11 end 12 13 defp unmatched(_), do: :ok 14 15 defp unmatched(:error), do: :error 16 end 17 """ 18 19 @behaviour Dialyxir.Warning 20 21 @impl Dialyxir.Warning 22 @spec warning() :: :pattern_match_cov 23 def warning(), do: :pattern_match_cov 24 25 @impl Dialyxir.Warning 26 @spec format_short([String.t()]) :: String.t() 27 def format_short([pattern, _]) do 28 "The pattern #{pattern} can never match the type, " <> 29 "because it is covered by previous clauses." 30 end 31 32 @impl Dialyxir.Warning 33 @spec format_long([String.t()]) :: String.t() 34 def format_long([pattern, type]) do 35 pretty_pattern = Erlex.pretty_print_pattern(pattern) 36 pretty_type = Erlex.pretty_print_type(type) 37 38 """ 39 The pattern 40 #{pretty_pattern} 41 42 can never match, because previous clauses completely cover the type 43 #{pretty_type}. 44 """ 45 end 46 47 @impl Dialyxir.Warning 48 @spec explain() :: String.t() 49 def explain() do 50 @moduledoc 51 end 52 end