LexCredo.Check.Warning.NonBooleanWithStrictOperator (LexCredo v0.2.1)

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 &&/||/! instead of and/or/not when an operand is a clearly non-boolean value.

The strict boolean operators raise an ArgumentError at runtime if the left operand is not exactly true or false. When an operand is a non-boolean literal (integer, float, string) or a non-boolean atom such as nil, :ok, or :error, the expression is truthy/falsy short-circuit logic and &&/||/! are the correct operators.

This check is intentionally narrow: struct field accesses (e.g. user.active) and unknown function calls are not flagged because their types cannot be determined statically, and boolean fields without a ? suffix are common in Elixir (e.g. Ecto schemas).

This check is the complement of PreferBooleanOperators, which flags &&/||/! when both operands are clearly boolean.

# BAD — right operand is a non-boolean atom; and/or raises or misleads
process(data) and :ok
result or :error

# BAD — string used as a fallback with or; use ||
env_var or "default"

# BAD — not requires a boolean argument; nil/atoms are not booleans
not nil

# GOOD
process(data) && :ok
result || :error
env_var || "default"
!nil

# NOT flagged — field access; type cannot be determined statically
# (user.active is a common boolean field pattern in Ecto schemas)
user.active and is_admin?(user)
exten == number and record.id

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.