MISRA C 2012: Rule-2.4

Viewed 29

This rule states that a project should not contain unused tag declarations.

Example:
typedef struct record_s     /* Non-compliant  */    
{
    unsigned short ax;
    unsigned short bx;
} record1_t;


typedef struct             /* Compliant */                      
{
    unsigned short ax;
    unsigned short bx;
} record2_t;

record1_t myRecord1_t;
record2_t myRecord2_t;

How does this makes compliant? What could be the possible issues if there is unused tag declarations?

1 Answers

The Rationale for the Rule is give in The Book:

If a tag is declared but not used, then it is unclear to a reviewer if the tag is redundant or it has been left unused by mistake.

Or to put it simply, if you are defining a tag, but not using is, why are you defining it in the first place?

An unused (and un-needed) tag is harmless... but an unused (but should have been used) tag may cause problems.

The Rule is Advisory, so can be disapplied if necessary. This is particularly applicable if you are adopting existing code (ie a third party header file) that includes tags that were used in the legacy code but are not needed in the new project.

Related