[previous] MISRA-C:2004  Rule  13.1:  (Required) [next] Assignment operators shall not be used in expressions that yield a Boolean value.
No assignments are permitted in any expression which is considered to have a Boolean value. This precludes the use of both simple and compound assignment operators in the operands of a Boolean-valued expression. However, it does not preclude assigning a Boolean value to a variable.

If assignments are required in the operands of a Boolean-valued expression then they must be performed separately outside of those operands. This helps to avoid getting '=' and '==' confused, and assists the static detection of mistakes.

See 'Boolean expressions' in the glossary.

For example write:

x = y;
if ( x != 0 )
{
    foo();
}

and not:

if ( ( x = y ) != 0 )   /* Boolean by context */
{
    foo();
}

or even worse:

if ( x = y )
{
    foo();
}


QAC messages that encompass this guideline:

3326 The result of an assignment is being used in a logical operation.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents