Main Content

MISRA C:2023 Rule 5.2

Identifiers declared in the same scope and name space shall be distinct

Since R2024a

Description

Rule Definition

Identifiers declared in the same scope and name space shall be distinct.

Rationale

If the two long names are almost identical and differ only in the later part, they can be easily mistaken for each other. The readability of the code is reduced.

Polyspace Implementation

Polyspace® considers two names as distinct if there is a difference between their first 63 characters. In C90, the difference must occur between the first 31 characters. To use the C90 rules checking, use the value c90 for the option C standard version (-c-version). You can change the number of characters compared using the option -code-behavior-specifications. See -code-behavior-specifications.

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

        
extern int engine_exhaust_gas_temperature_raw;
static int engine_exhaust_gas_temperature_scaled;      /* Non-compliant */

extern double raw_engine_exhaust_gas_temperature;
static double scaled_engine_exhaust_gas_temperature;  /* Compliant */

void func ( void )
{
  /* Not in the same scope */
  int engine_exhaust_gas_temperature_local;            /* Compliant */
}

In this example, the identifier engine_exhaust_gas_temperature_scaled has the same 31 characters as a previous identifier, engine_exhaust_gas_temperature_raw.

The rule does not apply if the two identifiers have the same 31 characters but have different scopes. For instance, engine_exhaust_gas_temperature_local has the same 31 characters as engine_exhaust_gas_temperature_raw but different scope.

        
extern int engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_raw;
static int engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_scale; /* Non-compliant */

    
extern int engine_gas_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx__raw;
static int engine_gas_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx__scale; /* Compliant */

void func ( void )
{
/* Not in the same scope */
    int engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_local; /* Compliant */
}

In this example, the identifier engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_scale has the same 63 characters as a previous identifier, engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_raw.

Check Information

Group: Identifiers
Category: Required
AGC Category: Required

Version History

Introduced in R2024a