[previous] 3396 [next] Extra parentheses recommended. A binary operation is the operand of a conditional operator.
Readability

The order in which operators are processed in this expression is not sufficiently clear and so additional parentheses are recommended. In the absence of parentheses, the sequence is determined by rules of precedence and associativity.

One operand of this conditional operator is a binary expression which is not parenthesised. Add parentheses to make it clear that the binary operator is evaluated first.

For example:


/*PRQA S 2017,3198,3199,3203,3344,3408,3401,3447 ++*/

extern int a;
extern int b;
extern int c;
extern int d;

extern void foo(void)
{
    int r;

    r = a ? b : c;                      /* OK                  */

    r = -a ? b : c;                     /* OK                  */

    r = a ? -b : c;                     /* OK                  */

    r = a > b ? c : d;                  /* Message 3396 + 3390 */
    r = (a > b) ? c : d;                /* OK                  */

    r = a + b ? c : d;                  /* Message 3396 + 3390 */
    r = (a + b) ? c : d;                /* OK                  */

    r = a ? b + c : d;                  /* Message 3396 + 3390 */
    r = a ? (b + c) : d;                /* OK                  */

    r = a ? b : c + d;                  /* Message 3396 + 3390 */
    r = a ? b : (c + d);                /* OK                  */
}


MISRA C:2012 Rules applicable to message 3396:

Rule-12.1  (Advisory) The precedence of operators within expressions should be made explicit


See also:

QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents