From 9ff6f73f838a1f90d09922448c434033ba5e094e Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Mon, 9 Oct 2023 00:36:48 +0600 Subject: refactor: allow not having a `default` case for enum Problem: The style guide states that all switch statements that are not conditional on an enum must have a `default` case, but does not give any explicit guideline for switch statements that are conditional on enums. As a result, a `default` case is added in many enum switch statements, even when the switch statement is exhaustive. This is not ideal because it removes the ability to have compiler errors to easily detect unchanged switch statements when a new possible value for an enum is added. Solution: Add explicit guidelines for switch statements that are conditional on an enum, clarifying that a `default` case is not necessary if the switch statement is exhaustive. Also refactor pre-existing code with unnecessary `default` cases. --- runtime/doc/dev_style.txt | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'runtime') diff --git a/runtime/doc/dev_style.txt b/runtime/doc/dev_style.txt index 79e758a11e..0bc27ced15 100644 --- a/runtime/doc/dev_style.txt +++ b/runtime/doc/dev_style.txt @@ -859,6 +859,33 @@ execute, simply use `abort()`: >c abort(); } +Switch statements that are conditional on an enumerated value should not have +a `default` case if it is exhaustive. Explicit case labels are preferred over +`default`, even if it leads to multiple case labels for the same code. For +example, instead of: >c + + case A: + ... + case B: + ... + case C: + ... + default: + ... + +You should use: >c + + case A: + ... + case B: + ... + case C: + ... + case D: + case E: + case F: + ... + Return Values ~ Do not needlessly surround the `return` expression with parentheses. -- cgit