rspec/rules/S1764/java/rule.adoc

95 lines
2.2 KiB
Plaintext

== Why is this an issue?
include::../description.adoc[]
=== Noncompliant code example
[source,java]
----
if ( a == a ) { // always true
doZ();
}
if ( a != a ) { // always false
doY();
}
if ( a == b && a == b ) { // if the first one is true, the second one is too
doX();
}
if ( a == b || a == b ) { // if the first one is true, the second one is too
doW();
}
int j = 5 / 5; //always 1
int k = 5 - 5; //always 0
c.equals(c); //always true
----
=== Exceptions
* This rule ignores ``++*++``, ``+``, and ``++=++``.
* The specific case of testing a floating point value against itself is a valid test for ``++NaN++`` and is therefore ignored.
* Similarly, left-shifting 1 onto 1 is common in the construction of bit masks, and is ignored.
[source,java]
----
float f;
if(f != f) { //test for NaN value
System.out.println("f is NaN");
}
int i = 1 << 1; // Compliant
int j = a << a; // Noncompliant
----
== Resources
* https://wiki.sei.cmu.edu/confluence/x/5dUxBQ[CERT, MSC12-C.] - Detect and remove code that has no effect or is never executed
* S1656 - Implements a check on ``++=++``.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
=== on 21 Nov 2024, 16:48:00 Alban Auzeill wrote:
[test-code-support-investigation-for-java] Decision for scope: Keep 'Main'. Tests do this to validate `equals`.
=== on 1 Oct 2014, 11:39:51 Nicolas Peru wrote:
Sub task for RSPEC-1764 with updated description for Java, please review.
=== on 1 Oct 2014, 11:44:49 Ann Campbell wrote:
\[~nicolas.peru] I question retaining this noncompliant example:
----
if (5 / 5) { // always 1
doV();
}
----
In C, >=1 evaluates to true & 0 evaluates to false. Is it that the same holds true in Java but it's just considered very bad form? If so, then why eliminate this one:
----
if (5 - 5) { // always 0
do_u();
}
----
?
Also, we have a different rule that yells at people for using equality operators with floating-point numbers, so the first exception is doubly confusing
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]