Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gaps in enums
#1
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)