opaque_match.ex (1621B)
1 defmodule Dialyxir.Warnings.OpaqueMatch do 2 @moduledoc """ 3 Attempted to pattern match against the internal structure of an 4 opaque term. 5 6 ## Example 7 8 defmodule OpaqueStruct do 9 defstruct [:opaque] 10 11 @opaque t :: %__MODULE__{} 12 13 @spec opaque() :: t 14 def opaque() do 15 %__MODULE__{} 16 end 17 end 18 19 defmodule Example do 20 @spec error() :: :error 21 def error() do 22 %{opaque: _} = OpaqueStruct.opaque() 23 :error 24 end 25 end 26 """ 27 28 @behaviour Dialyxir.Warning 29 30 @impl Dialyxir.Warning 31 @spec warning() :: :opaque_match 32 def warning(), do: :opaque_match 33 34 @impl Dialyxir.Warning 35 @spec format_short([String.t()]) :: String.t() 36 def format_short([_pattern, type | _]) do 37 pretty_type = Erlex.pretty_print_type(type) 38 39 "Attempted to pattern match against the internal structure of an opaque term of type #{pretty_type}." 40 end 41 42 @impl Dialyxir.Warning 43 @spec format_long([String.t()]) :: String.t() 44 def format_long([pattern, opaque_type, opaque_term]) do 45 pretty_opaque_term = Erlex.pretty_print(opaque_term) 46 47 term = 48 if opaque_type == opaque_term do 49 "the term" 50 else 51 pretty_opaque_term 52 end 53 54 pretty_pattern = Erlex.pretty_print_pattern(pattern) 55 56 """ 57 Attempted to pattern match against the internal structure of an opaque term. 58 59 Type: 60 #{pretty_opaque_term} 61 62 Pattern: 63 #{pretty_pattern} 64 65 This breaks the opaqueness of #{term}. 66 """ 67 end 68 69 @impl Dialyxir.Warning 70 @spec explain() :: String.t() 71 def explain() do 72 @moduledoc 73 end 74 end