brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · f164f4c Raw
99 lines · plain
1.. title:: clang-tidy - hicpp-multiway-paths-covered2 3hicpp-multiway-paths-covered4============================5 6This check discovers situations where code paths are not fully-covered.7It furthermore suggests using ``if`` instead of ``switch`` if the code8will be more clear.9The `rule 6.1.2 <https://www.perforce.com/resources/qac/high-integrity-cpp-coding-standard/statements>`_10and `rule 6.1.4 <https://www.perforce.com/resources/qac/high-integrity-cpp-coding-standard/statements>`_11of the High Integrity C++ Coding Standard are enforced.12 13``if-else if`` chains that miss a final ``else`` branch might lead to14unexpected program execution and be the result of a logical error.15If the missing ``else`` branch is intended you can leave it empty with16a clarifying comment.17This warning can be noisy on some code bases, so it is disabled by default.18 19.. code-block:: c++20 21  void f1() {22    int i = determineTheNumber();23 24     if(i > 0) {25       // Some Calculation26     } else if (i < 0) {27       // Precondition violated or something else.28     }29     // ...30  }31 32Similar arguments hold for ``switch`` statements which do not cover all33possible code paths.34 35.. code-block:: c++36 37  // The missing default branch might be a logical error. It can be kept empty38  // if there is nothing to do, making it explicit.39  void f2(int i) {40    switch (i) {41    case 0: // something42      break;43    case 1: // something else44      break;45    }46    // All other numbers?47  }48 49  // Violates this rule as well, but already emits a compiler warning (-Wswitch).50  enum Color { Red, Green, Blue, Yellow };51  void f3(enum Color c) {52    switch (c) {53    case Red: // We can't drive for now.54      break;55    case Green:  // We are allowed to drive.56      break;57    }58    // Other cases missing59  }60 61 62The `rule 6.1.4 <https://www.perforce.com/resources/qac/high-integrity-cpp-coding-standard/statements>`_63requires every ``switch`` statement to have at least two ``case`` labels other than a `default` label.64Otherwise, the ``switch`` could be better expressed with an ``if`` statement.65Degenerated ``switch`` statements without any labels are caught as well.66 67.. code-block:: c++68 69  // Degenerated switch that could be better written as `if`70  int i = 42;71  switch(i) {72    case 1: // do something here73    default: // do something else here74  }75 76  // Should rather be the following:77  if (i == 1) {78    // do something here79  }80  else {81    // do something here82  }83 84 85.. code-block:: c++86 87  // A completely degenerated switch will be diagnosed.88  int i = 42;89  switch(i) {}90 91 92Options93-------94 95.. option:: WarnOnMissingElse96 97  Boolean flag that activates a warning for missing ``else`` branches.98  Default is `false`.99