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 operands are expressions that have side effects. Polyspace® assumes:
Expressions that modifies at least one of its variables have side effects.
Explicit constructors or conversion functions that are declared but not defined have no side effects. Defined conversion functions have side effects.
Volatile accesses and function calls have no side effects.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Expressions |
Category: Required |
Version History
Introduced in R2013b