CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

Mismatch between the synopsis and enforcement of rule Enum.2

Open jansen-tiobe opened this issue 2 years ago • 5 comments

The synopsis of rule Enum.2 is "Use enumerations to represent sets of related named constants" and the enforcement of this rule is about flagging switch statements for enumerations that don't cover all enumerators. The enforcements certainly make sense but if you violate these it doesn't mean that you haven't "Used enumerations that don't represent sets of related named constants". These are completely unrelated.

Since I think the enforcements are great, they should be kept. It would better to change the synopsis to something like "Make sure all cases are covered when using a switch statement".

Note that a third enforcement could be added in this context:

  • Flag switch-statements where the cases cover all enumerators and there is a default.

The reason for this is that the default is actually dead code in such a case. But more importantly, the other enforcements will not trigger if there is a default because the default will cover the new enumerator.

jansen-tiobe avatar Aug 15 '23 19:08 jansen-tiobe

Note that a third enforcement could be added in this context:

  • Flag switch-statements where the cases cover all enumerators and there is a default.

The reason for this is that the default is actually dead code in such a case.

I think that it is not necessarily dead code, it depends on whether there's break inside the case.

iglesias avatar Jan 08 '24 14:01 iglesias

Agreed, although this is considered bad practice in most cases.

Note that my request is not about this extra enforcement but making sure the rule synopsis and enforcements are aligned. Can you say something about that?

pauljansen42 avatar Jan 08 '24 22:01 pauljansen42

Agreed, although this is considered bad practice in most cases.

Note that my request is not about this extra enforcement but making sure the rule synopsis and enforcements are aligned. Can you say something about that?

I already noticed that your "request" contained different aspects.

I think you might be onto something and it would be nice to see a better suggestion or change request. Further, I think it doesn't help when you are getting something wrong and someone tells you about it, that you reply, "agreed, but is considered bad practice" without supporting your claims.

Good luck!

iglesias avatar Jan 09 '24 07:01 iglesias

Thanks for your answer.

Concerning the "Agreed, although this is considered bad practice in most cases." I thought the evidence was clear. If you don't have a "break" (or any other control flow changer such as "throw", "return", ...) at the end of a non-empty case in a switch statement, you will get a fall through, which is warned about by almost all compilers e.g. -Wimplicit-fallthrough by gcc and clang.

But once again, this request is to have the enforcement and the synopsis aligned for rule Enum.2. Can this be picked up?

pauljansen42 avatar Jan 09 '24 18:01 pauljansen42

[sorry for using both my private "pauljansen42" and my company "jansen-tiobe" account]

Is there any update on this? The question is whether we can make the intention of rule ENUM.2 and its enforcements consistent. Now, the synopsis/intention and its enforcements are dealing with different things. I recommend to have 2 rules instead:

  • (current Enum.2) Use enumerations to represent sets of related named constants
  • (new rule) Make sure all enumerators are taken into consideration in a switch statement

I am willing to write the text for this new rule. Let me know.

jansen-tiobe avatar Jan 01 '25 23:01 jansen-tiobe