Main Content

MISRA C:2012 Rule 5.6

A typedef name shall be a unique identifier

Description

Rule Definition

A typedef name shall be a unique identifier.

Rationale

Reusing a typedef name as another typedef or as the name of a function, object or enum constant can cause developer confusion.

Additional Message in Report

XX conflicts with the typedef name YY.

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 func ( void ){
  {
    typedef unsigned char u8_t;
  }
  {
    typedef unsigned char u8_t; /* Non-compliant */
  }
}

typedef float mass;
void func1 ( void ){
  float mass = 0.0f;            /* Non-compliant */
}

In this example, the typedef name u8_t is used twice. The typedef name mass is also used as an identifier name.

typedef struct list{          /* Compliant - exception */				
  struct list *next;
  unsigned short element;
} list;												

typedef struct{
  struct chain{               /* Non-compliant */	
    struct chain *list2;
    unsigned short element;
  } s1;
  unsigned short length;
} chain;

In this example, the typedef name list is the same as the original name of the struct type. The rule allows this exceptional case.

However, the typedef name chain is not the same as the original name of the struct type. The name chain is associated with a different struct type. Therefore, it clashes with the typedef name.

Check Information

Group: Identifiers
Category: Required
AGC Category: Required

Version History

Introduced in R2014b