Main Content

MISRA C:2012 Rule 13.4

The result of an assignment operator should not be used

Description

Rule Definition

The result of an assignment operator should not be used.

Rationale

The rule is violated if the following happens in the same expression:

  • The assignment operator acts on a variable.

  • Another read or operation is performed on the result of the assignment.

For example, the expression a[x]=a[x=y]; violates this rule. The [] operator acts on the result of the assignment x=y.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

int x, y, b, c, d;
int a[10];
unsigned int bool_var, false=0, true=1;

int foo(void) {

    x = y;            /* Compliant - x is not used */

    a[x] = a[x = y];  /* Non-compliant - Value of x=y is used */

    if ( bool_var = false )/* Non-compliant - bool_var=false is used */
{}


    if ( bool_var == false ) {}   /* Compliant */

    if ( ( 0u == 0u ) || ( bool_var = true ) )/* Non-compliant */
                    /*- even though (bool_var=true) is not evaluated */
 {} 
    

    if ( ( x = f () ) != 0 )/* Non-compliant - value of x=f() is used */
 {} 
    a[b += c] = a[b];/* Non-compliant - value of b += c is used */
    b = c = d = 0; /* Non-compliant - value of d=0 and c=d=0 are used */

}

In this example, the rule is violated when the result of an assignment is used.

Check Information

Group: Side Effects
Category: Advisory
AGC Category: Advisory

Version History

Introduced in R2014b

expand all