LexCredo.Check.Warning.UsePositiveTypeGuards (LexCredo v0.1.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

Use positive type guards instead of negated type checks in function guards.

Negating a type guard is imprecise — not is_nil(x) passes integers, lists, and anything else that is not nil. not is_binary(x) passes nil, integers, atoms, and more. State what you expect, not what you want to exclude.

# BAD: passes any non-nil value, including integers, lists, etc.
def call_service(%{req: req}) when not is_nil(req), do: ...
def call_service(%{req: req}) when req != nil, do: ...

# BAD: passes nil, integers, atoms, …
def validate(x) when not is_binary(x), do: ...

# GOOD: precise about what is actually expected
def call_service(%{req: req}) when is_binary(req), do: ...

Check-Specific Parameters

Use the following parameters to configure this check:

:exclude_test_files

When true, skips test files. Default: false.

This parameter defaults to false.

General Parameters

Like with all checks, general params can be applied.

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