81 lines
1.5 KiB
Plaintext
81 lines
1.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
include::../description.adoc[]
|
|
|
|
=== Exceptions
|
|
|
|
This rule ignores assignments in conditions of `while` statements and assignments enclosed in relational expressions.
|
|
|
|
[source,java]
|
|
----
|
|
void processInput(BufferedReader br) {
|
|
String line;
|
|
while ((line = br.readLine()) != null) {
|
|
processLine(line);
|
|
}
|
|
}
|
|
|
|
Object foo;
|
|
if ((foo = bar()) != null) {
|
|
// do something with "foo"
|
|
}
|
|
----
|
|
|
|
This rule also ignores chained assignments, including compound assignments.
|
|
|
|
[source,java]
|
|
----
|
|
int j, i = j = 0;
|
|
int k = (j += 1);
|
|
byte[] result, bresult;
|
|
result = (bresult = new byte[len]);
|
|
----
|
|
|
|
== How to fix it
|
|
|
|
include::../how-to-fix-it.adoc[]
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
String str;
|
|
if (!(str = cont.substring(pos1, pos2)).isEmpty()) { // Noncompliant
|
|
// do something with "str"
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
String str = cont.substring(pos1, pos2);
|
|
if (!str.isEmpty()) {
|
|
// do something with "str"
|
|
}
|
|
----
|
|
|
|
include::../see.adoc[]
|
|
|
|
* https://wiki.sei.cmu.edu/confluence/x/ITZGBQ[CERT, EXP51-J.] - Do not perform assignments in conditional expressions
|
|
|
|
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)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|