org.sonar.l10n.java.rules.squid.S2676.html Maven / Gradle / Ivy
It is possible for a call to hashCode
to return Integer.MIN_VALUE
. Take the absolute value of such a hashcode and you'll still have a negative number. Since your code is likely to assume that it's a positive value instead, your results will be unreliable.
Similarly, Integer.MIN_VALUE
could be returned from Random.nextInt()
or any object's compareTo
method, and Long.MIN_VALUE
could be returned from Random.nextLong()
. Calling Math.abs
on values returned from these methods is similarly ill-advised.
Noncompliant Code Example
public void doSomething(String str) {
if (Math.abs(str.hashCode()) > 0) { // Noncompliant
// ...
}
}
Compliant Solution
public void doSomething(String str) {
if (str.hashCode() != 0) {
// ...
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy