2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2020-06-30 12:47:33 +02:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Exceptions
|
2020-06-30 12:47:33 +02:00
|
|
|
|
2023-10-13 19:17:01 +02:00
|
|
|
This rule ignores assignments in conditions of `while` statements and assignments enclosed in relational expressions.
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2023-10-13 19:17:01 +02:00
|
|
|
[source,java]
|
2020-06-30 12:47:33 +02:00
|
|
|
----
|
2023-10-13 19:17:01 +02:00
|
|
|
void processInput(BufferedReader br) {
|
|
|
|
String line;
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
processLine(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object foo;
|
|
|
|
if ((foo = bar()) != null) {
|
|
|
|
// do something with "foo"
|
|
|
|
}
|
2020-06-30 12:47:33 +02:00
|
|
|
----
|
|
|
|
|
2023-10-13 19:17:01 +02:00
|
|
|
This rule also ignores chained assignments, including compound assignments.
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2023-10-13 19:17:01 +02:00
|
|
|
[source,java]
|
2020-06-30 12:47:33 +02:00
|
|
|
----
|
2023-10-13 19:17:01 +02:00
|
|
|
int j, i = j = 0;
|
2020-06-30 12:47:33 +02:00
|
|
|
int k = (j += 1);
|
2023-10-13 19:17:01 +02:00
|
|
|
byte[] result, bresult;
|
2020-06-30 12:47:33 +02:00
|
|
|
result = (bresult = new byte[len]);
|
|
|
|
----
|
|
|
|
|
2023-10-13 19:17:01 +02:00
|
|
|
== 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[]
|
2021-09-21 15:40:35 +02:00
|
|
|
|
|
|
|
* https://wiki.sei.cmu.edu/confluence/x/ITZGBQ[CERT, EXP51-J.] - Do not perform assignments in conditional expressions
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../message.adoc[]
|
|
|
|
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
2023-06-22 10:38:01 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|