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 indicates that an expression can be trivially simplified to a more concise form without changing its behavior. This diagnostic helps improve code readability and can be especially useful during code refactoring and optimization.

Supported Simplifications

List Operations

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

Arithmetic Operations

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

Boolean Operations

  • Short-circuit evaluation: true andalso XX and false orelse XX
  • Negation of literals: not truefalse and not falsetrue

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.