1 .. title:: clang-tidy - bugprone-branch-clone
6 Checks for repeated branches in ``if/else if/else`` chains, consecutive
7 repeated branches in ``switch`` statements and identical true and false
8 branches in conditional operators.
20 In this simple example (which could arise e.g. as a copy-paste error) the
21 ``then`` and ``else`` branches are identical and the code is equivalent the
22 following shorter and cleaner code:
26 test_value(x); // can be omitted unless it has side effects
31 If this is the intended behavior, then there is no reason to use a conditional
32 statement; otherwise the issue can be solved by fixing the branch that is
35 The check detects repeated branches in longer ``if/else if/else`` chains
36 where it would be even harder to notice the problem.
38 The check also detects repeated inner and outer ``if`` statements that may
39 be a result of a copy-paste error. This check cannot currently detect
40 identical inner and outer ``if`` statements if code is between the ``if``
41 conditions. An example is as follows.
45 void test_warn_inner_if_1(int x) {
46 if (x == 1) { // warns, if with identical inner if
47 if (x == 1) // inner if is here
49 if (x == 1) { // does not warn, cannot detect
57 In ``switch`` statements the check only reports repeated branches when they are
58 consecutive, because it is relatively common that the ``case:`` labels have
59 some natural ordering and rearranging them would decrease the readability of
60 the code. For example:
77 Here the check reports that the ``'a'`` and ``'A'`` branches are identical
78 (and that the ``'b'`` and ``'B'`` branches are also identical), but does not
79 report that the ``default:`` branch is also identical to the first two branches.
80 If this is indeed the correct behavior, then it could be implemented as:
95 Here the check does not warn for the repeated ``return 10;``, which is good if
96 we want to preserve that ``'a'`` is before ``'b'`` and ``default:`` is the last
99 Switch cases marked with the ``[[fallthrough]]`` attribute are ignored.
101 Finally, the check also examines conditional operators and reports code like:
105 return test_value(x) ? x : x;
107 Unlike if statements, the check does not detect chains of conditional
110 Note: This check also reports situations where branches become identical only