This rule involves the use of Math.abs
and negation on numbers that could be MIN_VALUE
. It is a problem because it can
lead to incorrect results and unexpected behavior in the program.
When Math.abs
and negation are used on numbers that could be MIN_VALUE
, the result can be incorrect due to integer
overflow. Common methods that can return a MIN_VALUE
and raise an issue when used together with Math.abs
are:
Random.nextInt()
and Random.nextLong()
hashCode()
compareTo()
Alternatively, the absExact()
method throws an ArithmeticException
for MIN_VALUE
.
public void doSomething(String str) { if (Math.abs(str.hashCode()) > 0) { // Noncompliant // ... } }
public void doSomething(String str) { if (str.hashCode() != 0) { // ... } }