AUTOSAR C++14 Rule M5-14-1
The right hand operand of a logical &&, || operators shall not contain side effects
Description
Rule Definition
The right hand operand of a logical &&, || operators shall not contain side effects.
Rationale
When evaluated, an expression that has side effects modifies at least one of the
variables in the expression. For instance, n++
is an expression with side
effect.
The right 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 operands are not evaluated. Side effects of the expression do not take place. If your program relies on such side effects, you might see unexpected results.
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, Automated |
Version History
Introduced in R2019a