Bylaw.Credo.Check.Elixir.FullySpecifiedStructTypes (bylaw_credo v0.4.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Fully specify struct fields in type declarations instead of using empty struct literals such as %__MODULE__{}.

Explicit fields make the type communicate which data callers may rely on and let Dialyzer and readers catch incomplete or incompatible struct usage. An empty literal hides that contract behind the implementation's current struct definition.

Examples

Avoid:

  @type t :: %__MODULE__{}
  @opaque result :: {:ok, %URI{}}

Prefer:

  @type t :: %__MODULE__{id: integer(), name: String.t()}
  @opaque result :: {:ok, %URI{host: String.t() | nil, path: String.t()}}

Notes

This check uses static AST analysis, so it favors clear source-level patterns over runtime behavior.

Options

This check has no check-specific options. Configure it with an empty option list.

Usage

Add this check to Credo's checks: list in .credo.exs:

%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Elixir.FullySpecifiedStructTypes, []}
      ]
    }
  ]
}

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.