// There is no need to consider the symmetric case as it requires that all expression in the
// rhs (except the last one) to be side effect free, in which case they will end up being
// removed anyway.
List<JExpression> expressions = ((JMultiExpression) lhs).getExpressions();
JMultiExpression result = new JMultiExpression(lhs.getSourceInfo(),
expressions.subList(0, expressions.size() - 1));
result.addExpressions(new JBinaryOperation(x.getSourceInfo(), x.getType(), x.getOp(),
expressions.get(expressions.size() - 1), rhs));
ctx.replaceMe(result);
return;
}
if (isNonEmptyMultiExpression(rhs) && lhs instanceof JValueLiteral &&
op != JBinaryOperator.AND && op != JBinaryOperator.OR) {
// Push the operation inside the multiexpression if the lhs is a value literal.
// This exposes other optimization opportunities for latter passes e.g:
//
// 2 + (a(), b(), 1) ==> (a(), b(), 2 + 1) ==> (a(), b(), 3)
//
// And exception must be made for || and &&, as these operations might not evaluate the
// rhs due to shortcutting.
List<JExpression> expressions = ((JMultiExpression) rhs).getExpressions();
JMultiExpression result = new JMultiExpression(rhs.getSourceInfo(),
expressions.subList(0, expressions.size() - 1));
result.addExpressions(new JBinaryOperation(x.getSourceInfo(), x.getType(), x.getOp(),
lhs, expressions.get(expressions.size() - 1)));
ctx.replaceMe(result);
return;
}