rspec/rules/S2185/java/rule.adoc

65 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Certain math operations are just silly and should not be performed because their results are predictable.
In particular, ``++anyValue % 1++`` is silly because it will always return 0.
Casting a non-floating-point value to floating-point and then passing it to ``++Math.round++``, ``++Math.ceil++``, or ``++Math.floor++`` is silly because the result will always be the original value.
These operations are silly with any constant value: ``++Math.abs++``, ``++Math.ceil++``, ``++Math.floor++``, ``++Math.rint++``, ``++Math.round++``.
And these oprations are silly with certain constant values:
[frame=all]
[cols="^1,^1"]
|===
|Operation|Value
|acos|0.0 or 1.0
|asin|0.0 or 1.0
|atan|0.0 or 1.0
|atan2|0.0
|cbrt|0.0 or 1.0
|cos|0.0
|cosh|0.0
|exp|0.0 or 1.0
|expm1|0.0
|log|0.0 or 1.0
|log10|0.0 or 1.0
|sin|0.0
|sinh|0.0
|sqrt|0.0 or 1.0
|tan|0.0
|tanh|0.0
|toDegrees|0.0 or 1.0
|toRadians|0.0
|===
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public void doMath(int a) {
double floor = Math.floor((double)a); // Noncompliant
double ceiling = Math.ceil(4.2); // Noncompliant
double arcTan = Math.atan(0.0); // Noncompliant
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this silly call to "Math.xxx".
endif::env-github,rspecator-view[]