MISRA C:2023 Rule 23.7
A generic selection that is expanded from a macro should evaluate its argument only once
Since R2024a
Description
Rule Definition
A generic selection that is expanded from a macro should evaluate its argument only once.
Rationale
Because the controlling argument of a generic selection is not evaluated, inconsistently evaluating the expressions in the association list can lead to unexpected results. For example, if evaluating the macro argument has a side effect and it is evaluated inconsistently in the association list, then the side effect might not be invoked for certain selections.
If your generic selection is expanded from a macro, evaluate the macro argument in the association list once regardless of which association is selected. For consistent evaluation of the argument, evaluate the argument outside of the generic selection association list.
Polyspace Implementation
Polyspace® reports a violation of this rule if a generic selection is expanded from a macro and at least one expression in the association list does not evaluate the macro argument.
If none of the expressions in the association list evaluates the argument, Polyspace does not report a violation. As an exception, this rule is not violated if all expressions in the association list are constant expressions.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Generic Selections |
Category: Advisory |
AGC Category: Advisory |
Version History
Introduced in R2024a