MISRA Discussion Forums
Gaps in enums - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: General Questions (https://forum.misra.org.uk/forumdisplay.php?fid=27)
+--- Thread: Gaps in enums (/showthread.php?tid=1750)



Gaps in enums - delirium5223 - 04-09-2025

Can we have a new rule that says 
Quote:  if enum is used as an iterator type then there shall not be gaps in enum values.




Code:
typedef enum
{
  PIN_A = 1, 
    //missing 2
  PIN_B = 3,
  PIN_C = 4,
  PIN_MAX,
} tPin;
 
for(tPin i = PIN_A ; i < PIN_MAX; i++) 
{
// i reaching 2 is unintended/refactoring/human error
}

Surely we would want to know/acknowledge that enum has a gap if we plan to iterate over it.
(regardless where the 'for' loop starts/end)
   
It would make a good higher level/abstraction rule.
 
Or at least make it advisory.