rspec/rules/S2676/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

45 lines
1.1 KiB
Plaintext

== Why is this an issue?
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
[source,java]
----
public void doSomething(String str) {
if (Math.abs(str.hashCode()) > 0) { // Noncompliant
// ...
}
}
----
=== Compliant solution
[source,java]
----
public void doSomething(String str) {
if (str.hashCode() != 0) {
// ...
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use the original value instead.
endif::env-github,rspecator-view[]