Skip to main content

W0019 - Expression can be simplified

Warning​

-module(main).
foo(X) ->
bar([] ++ [1]).
%% ^^^^^^^^^ 💡 warning: Can be simplified to `[1]`.

Explanation​

The warning identifies expressions that can usually be replaced with a more concise form. It applies syntactic simplification rules and does not prove that a replacement is equivalent for every possible Erlang term or floating-point representation. This diagnostic helps improve code readability and can be especially useful during code refactoring and optimization.

Supported Simplifications​

List Operations​

  • Append with empty list: [] ++ X → X and X ++ [] → X
  • Subtract with empty list: [] -- X → [] and X -- [] → X

Arithmetic Operations​

  • Addition with zero: 0 + X → X and X + 0 → X
  • Subtraction with zero: 0 - X → -X and X - 0 → X
  • Multiplication by one: 1 * X → X and X * 1 → X
  • Division by one: X div 1 → X
  • Remainder by one: X rem 1 → 0

Boolean Operations​

  • Short-circuit evaluation: true andalso X → X and false orelse X → X
  • Negation of literals: not true → false and not false → true

Semantic Caveats​

These simplifications assume conventional use of the operators with operands of the expected types. Some edge cases are observably different. For example:

  • Simplifying X + 0 to X preserves -0.0, whereas evaluating -0.0 + 0 produces positive 0.0. The original expression also raises badarith when X is not a number.
  • Simplifying X ++ [] to X removes the badarg raised when X is an improper list.

If code intentionally relies on an operator for normalization, conversion, runtime validation, or exception behavior, make that intention explicit or use the standard elp:ignore mechanism to suppress W0019 for the expression.

Note on Macros​

This diagnostic does not apply to expressions that involve macro expansions, as simplifying them might change the intended behavior or readability of the code.