LexCredo.Check.Warning.PreferBooleanOperators (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 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 at least one operand is clearly boolean-returning — i.e. an is_* guard call, a comparison operator, or another boolean operator.

# BAD — operands are boolean-typed
is_binary(x) && is_integer(y)
has_permission?(user) || is_admin?(user)
!is_nil(value)

# GOOD
is_binary(x) and is_integer(y)
has_permission?(user) or is_admin?(user)
not is_nil(value)

# NOT flagged — truthy/falsy short-circuit idiom, not boolean-typed
user && 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.