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 also detects repeated branches in longer ``if/else if/else`` chains
36 where it would be even harder to notice the problem.
38 In ``switch`` statements the check only reports repeated branches when they are
39 consecutive, because it is relatively common that the ``case:`` labels have
40 some natural ordering and rearranging them would decrease the readability of
41 the code. For example:
58 Here the check reports that the ``'a'`` and ``'A'`` branches are identical
59 (and that the ``'b'`` and ``'B'`` branches are also identical), but does not
60 report that the ``default:`` branch is also identical to the first two branches.
61 If this is indeed the correct behavior, then it could be implemented as:
76 Here the check does not warn for the repeated ``return 10;``, which is good if
77 we want to preserve that ``'a'`` is before ``'b'`` and ``default:`` is the last
80 Switch cases marked with the ``[[fallthrough]]`` attribute are ignored.
82 Finally, the check also examines conditional operators and reports code like:
86 return test_value(x) ? x : x;
88 Unlike if statements, the check does not detect chains of conditional
91 Note: This check also reports situations where branches become identical only