rspec/rules/S1121/java/rule.adoc

81 lines
1.5 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:47:33 +02:00
include::../description.adoc[]
=== Exceptions
2020-06-30 12:47:33 +02:00
This rule ignores assignments in conditions of `while` statements and assignments enclosed in relational expressions.
[source,java]
2020-06-30 12:47:33 +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
----
This rule also ignores chained assignments, including compound assignments.
[source,java]
2020-06-30 12:47:33 +02:00
----
int j, i = j = 0;
2020-06-30 12:47:33 +02:00
int k = (j += 1);
byte[] result, bresult;
2020-06-30 12:47:33 +02:00
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[]