rspec/rules/S2676/java/rule.adoc
2021-04-28 18:08:03 +02:00

29 lines
839 B
Plaintext

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) {
// ...
}
}
----