1 .. title:: clang-tidy - hicpp-multiway-paths-covered
3 hicpp-multiway-paths-covered
4 ============================
6 This check discovers situations where code paths are not fully-covered.
7 It furthermore suggests using ``if`` instead of ``switch`` if the code will be more clear.
8 The `rule 6.1.2 <http://www.codingstandard.com/rule/6-1-2-explicitly-cover-all-paths-through-multi-way-selection-statements/>`_
9 and `rule 6.1.4 <http://www.codingstandard.com/rule/6-1-4-ensure-that-a-switch-statement-has-at-least-two-case-labels-distinct-from-the-default-label/>`_
10 of the High Integrity C++ Coding Standard are enforced.
12 ``if-else if`` chains that miss a final ``else`` branch might lead to unexpected
13 program execution and be the result of a logical error.
14 If the missing ``else`` branch is intended you can leave it empty with a clarifying
16 This warning can be noisy on some code bases, so it is disabled by default.
21 int i = determineTheNumber();
26 // Precondition violated or something else.
31 Similar arguments hold for ``switch`` statements which do not cover all possible code paths.
35 // The missing default branch might be a logical error. It can be kept empty
36 // if there is nothing to do, making it explicit.
41 case 1: // something else
47 // Violates this rule as well, but already emits a compiler warning (-Wswitch).
48 enum Color { Red, Green, Blue, Yellow };
49 void f3(enum Color c) {
51 case Red: // We can't drive for now.
53 case Green: // We are allowed to drive.
56 // Other cases missing
60 The `rule 6.1.4 <http://www.codingstandard.com/rule/6-1-4-ensure-that-a-switch-statement-has-at-least-two-case-labels-distinct-from-the-default-label/>`_
61 requires every ``switch`` statement to have at least two ``case`` labels other than a `default` label.
62 Otherwise, the ``switch`` could be better expressed with an ``if`` statement.
63 Degenerated ``switch`` statements without any labels are caught as well.
67 // Degenerated switch that could be better written as `if`
70 case 1: // do something here
71 default: // do something else here
74 // Should rather be the following:
85 // A completely degenerated switch will be diagnosed.
93 .. option:: WarnOnMissingElse
95 Boolean flag that activates a warning for missing ``else`` branches.