Assignments should not be made from within sub-expressions

  • squid : AssignmentInSubExpressionCheck

Assignments within sub-expressions are hard to spot and therefore make the code less readable. It is also a common mistake to write = when == was meant. Ideally, expressions should not have side-effects.

Noncompliant Code Example

System.out.println(i = 42);

Compliant Solution

System.out.println(i == 42);

or:

i = 42;
System.out.println(i);

Exceptions

Assignments enclosed in relational expressions are allowed.

BufferedReader br = new BufferedReader(/* ... */);
String line;
while ((line = br.readLine()) != null) {
  /* ... */
}
Close