rspec/rules/S2154/java/rule.adoc
2022-02-04 16:28:24 +00:00

38 lines
864 B
Plaintext

If wrapped primitive values (e.g. ``++Integers++`` and ``++Floats++``) are used in a ternary operator (e.g. ``++a?b:c++``), both values will be unboxed and coerced to a common type, potentially leading to unexpected results. To avoid this, add an explicit cast to a compatible type.
== Noncompliant Code Example
[source,java]
----
Integer i = 123456789;
Float f = 1.0f;
Number n = condition ? i : f; // Noncompliant; i is coerced to float. n = 1.23456792E8
----
== Compliant Solution
[source,java]
----
Integer i = 123456789;
Float f = 1.0f;
Number n = condition ? (Number) i : f; // n = 123456789
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]