W0097 - Integer range check can be written with is_integer/3
Warning
f(X) when is_integer(X), X > 0, X < 10 -> X.
%% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: W0097: Integer range check can be written with `is_integer/3`.
Explanation
The code checks that a variable is an integer and compares it against a lower
and an upper bound. Since OTP 29, the is_integer/3 BIF does all of this in a
single call, which is allowed in guards: is_integer(X, Lo, Hi) is true when
X is an integer and Lo =< X =< Hi.
The warning applies to the tests of a guard (separated by ,) and to andalso
chains, both in function bodies and in macro definitions. It is only reported
when ELP runs with OTP 29 or later.
is_integer/3 raises badarg if a bound is not an integer, so the warning
only applies when both bounds are certainly integers: integer or character
literals, macros expanding to them, integer arithmetic on them (not /), and
variables checked with is_integer/1 in the same guard. In an andalso
chain, where is_integer/3 evaluates both bounds even if the original chain
would have stopped at an earlier false, a div or rem in a bound must
also have a non-zero literal divisor, since these raise on a zero divisor, and
a bsl or bsr a literal shift count of at most 1024, since a large shift
count raises system_limit.
The warning is not reported when both bounds are constant and the lower one,
made inclusive, is greater than the upper one: such a check is always false,
and the compiler rejects is_integer/3 with constant inverted bounds. It is
also not reported when ELP cannot tell whether constant bounds are inverted,
for example when both are beyond 128-bit integers.
To fix this warning, replace the is_integer/1 call with is_integer/3 and
remove the two comparisons. A strict bound becomes the adjacent inclusive
value:
f(X) when is_integer(X, 1, 9) -> X.