Main Content

MISRA C:2023 Rule 23.8

A default association shall appear as either the first or the last association of a generic selection

Since R2024a

Description

Rule Definition

A default association shall appear as either the first or the last association of a generic selection.

This rule comes from MISRA C™: 2012 Amendment 3.

Rationale

The code is easier to read and understand when the default association is either the first or the last association in a generic selection.

Polyspace Implementation

Polyspace® reports a violation of this rule if the default association of a _Generic statement is neither the first nor the last association.

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

expand all

In this example, the default association is neither the first nor the last association of get_type_id. Polyspace reports a violation of this rule.

 #define get_type_id(x) ( _Generic((x) /* Noncompliant */ \
                        , char : 0       \
                        , default : 3    \
	                    , int  : 1      ))


void foo() {
	int x, y;
	y = get_type_id(x);
}

Check Information

Group: Generic Selections
Category: Required
AGC Category: Required

Version History

Introduced in R2024a