Main Content

MISRA C:2023 Rule 2.6

A function should not contain unused label declarations

Since R2024a

Description

Rule Definition

A function should not contain unused label declarations.

Rationale

If you declare a label but do not use it, it is not clear to a reviewer of your code if the label is redundant or unused by mistake.

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

void use_var(signed short);

void unused_label ( void )
{
    signed short x = 6;

label1:                        /* Non-compliant - label1 not used */
    use_var ( x );
}

void used_label ( void )
{
    signed short x = 6;

    for (int i=0; i < 5; i++) {
        if ( i==2 ) goto label1;
    }

label1:		               /* Compliant - label1 used */
    use_var ( x );
}

In this example, the rule is violated when the label label1 in function unused_label is not used.

Check Information

Group: Unused code
Category: Advisory
AGC Category: Readability

Version History

Introduced in R2024a