rspec/rules/S2197/java/rule.adoc

45 lines
889 B
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
include::../description.adoc[]
=== Noncompliant code example
[source,java]
----
public boolean isOdd(int x) {
return x % 2 == 1; // Noncompliant; if x is an odd negative, x % 2 == -1
}
----
=== Compliant solution
[source,java]
----
public boolean isOdd(int x) {
return x % 2 != 0;
}
----
== Resources
* https://wiki.sei.cmu.edu/confluence/x/pDdGBQ[CERT, NUM51-J.] - Do not assume that the remainder operator always returns a nonnegative result for integral operands
* https://wiki.sei.cmu.edu/confluence/x/_NUxBQ[CERT, INT10-C] - Do not assume a positive remainder when using the % operator
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[]