MISRA C++:2023 Rule 8.14.1
The right-hand operand of a logical
&&
or
||
operator should not contain persistent side
effects
Since R2024b
Description
Rule Definition
The right-hand operand of a logical && or || operator should not contain persistent side effects.
Rationale
The right operand of the ||
operator is not evaluated if the left
operand is true. The right operand of the &&
operator is not
evaluated if the left operand is false. In these cases, if the right operand modifies
the value of a variable, the modification does not take place.
Polyspace Implementation
The rule checker reports situations where the right operand of a logical
||
or &&
operator has persistent side
effects. For example, if the right operand contains a function call and the function
modifies a global variable, the rule checker reports a violation.
The rule checker does not report a violation if the right operand contains a call to a pure function, that is, a function without side effects. The checker considers a function as pure if the function performs only simple operations such as:
Reading a nonvolatile parameter or global variable.
Writing to a local variable.
In addition to simple operations, if the function contains a call to another function, the checker attempts to determine if the called function is a pure function. If the called function is a pure function, the checker propagates this information and tags the calling function as a pure function (as long as the other operations in the calling function are simple operations).
The rule checker does not consider a function as pure if the function does any of these:
Writes to a global variable or dereferences a parameter
Reads or writes to a volatile variable, or contains an
asm
block
To determine if a function is pure, the checker needs to analyze the function definition. The checker looks for function definitions only within the same translation unit as the function call. A translation unit is a source file plus all headers included in the source. If a function definition is not found in the current translation unit, the checker does not report a violation of this rule.
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: Advisory |
Version History
Introduced in R2024b