LexCredo.Check.Warning.PreferBooleanOperators (LexCredo v0.2.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 normal and works with any version of Elixir.

Explanation

Use and/or/not instead of &&/||/! when operands are booleans.

The strict boolean operators make intent clear and catch accidental truthy values (e.g. :undefined from Erlang APIs). This check flags &&, ||, and ! when both operands are clearly boolean-returning — i.e. an is_* guard call, a comparison operator, a literal boolean, or another strict boolean operator.

Requiring both sides prevents false positives: and/or raise an ArgumentError if the left operand is not a boolean at runtime, so user && is_nil(user.name) must not be rewritten as user and is_nil(user.name)user can be nil.

# BAD — both operands are clearly boolean-returning
is_binary(x) && is_integer(y)
is_nil(x) || is_atom(x)
!is_nil(value)

# GOOD
is_binary(x) and is_integer(y)
is_nil(x) or is_atom(x)
not is_nil(value)

# NOT flagged — left side is not boolean-typed; &&/|| is the right tool
user && user.name
user && is_nil(user.name)
config[:timeout] || 5_000

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.