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→XandX ++ []→X - Subtract with empty list:
[] -- X→[]andX -- []→X
Arithmetic Operations
- Addition with zero:
0 + X→XandX + 0→X - Subtraction with zero:
0 - X→-XandX - 0→X - Multiplication by one:
1 * X→XandX * 1→X - Division by one:
X div 1→X - Remainder by one:
X rem 1→0
Boolean Operations
- Short-circuit evaluation:
true andalso X→Xandfalse orelse X→X - Negation of literals:
not true→falseandnot 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 + 0toXpreserves-0.0, whereas evaluating-0.0 + 0produces positive0.0. The original expression also raisesbadarithwhenXis not a number. - Simplifying
X ++ []toXremoves thebadargraised whenXis 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.