MISRA C++:2008 Rule 5-14-1

The right hand operand of a logical && or || operator shall not contain side effects

Description

Rule Definition

The right hand operand of a logical && or || operator shall not contain side effects.

Rationale

When evaluated, an expression with side effect modifies at least one of the variables in the expression. For instance, n++ is an expression with side effect.

The right-hand operand of a:

  • Logical && operator is evaluated only if the left-hand operand evaluates to true.

  • Logical || operator is evaluated only if the left-hand operand evaluates to false.

In other cases, the right-hand operands are not evaluated, so side effects of the expression do not take place. If your program relies on the side effects, you might see unexpected results in those cases.

Polyspace Implementation

The checker flags logical && or || operators whose right-hand operands are expressions with side effects.

The checker does not consider volatile accesses and function calls as potential side effects.

Troubleshooting

If you expect a rule violation but do not see it, refer to the documentation for Polyspace® Bug Finder™ or Polyspace Bug Finder Server™.

Check Information

Group: Expressions
Category: Required
Introduced in R2013b