Rule Definition
Assignments used in a sub-expression add an additional side effect to that of the full expression, potentially resulting in a value inconsistent with developer expectations. In addition, this helps to avoid getting = and == confused.
Remediation
Try splitting the expressions and assignments into multiple lines for better readability and maintainability.
Violation Code Sample
x = y;
x = y = z; // Non-compliant
if ( ( x = y ) != 0 ) // Non-compliant
{
foo ( );
}
if ( x = y ) // Non-compliant
{
foo ( );
}
Fixed Code Sample
if ( x != 0 ) // Compliant
{
foo ( );
}
bool b1 = x != y; // Compliant
bool b2;
b2 = x != y; // Compliant
if ( int16_t i = foo ( ) ) // Compliant
{
)
_________________________
x=y;
if ( x != 0 ) // compliant
{
foo ( );
}
Reference
Standards Reference:
MISRA C++ 2008, Rule 6–2–1: Assignment operators shall not be used in subexpressions.
Related Technologies
Technical Criterion
Complexity - Algorithmic and Control Structure Complexity
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.