File: branch-clone.rst

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (92 lines) | stat: -rw-r--r-- 2,513 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
.. title:: clang-tidy - bugprone-branch-clone

bugprone-branch-clone
=====================

Checks for repeated branches in ``if/else if/else`` chains, consecutive
repeated branches in ``switch`` statements and identical true and false
branches in conditional operators.

.. code-block:: c++

    if (test_value(x)) {
      y++;
      do_something(x, y);
    } else {
      y++;
      do_something(x, y);
    }

In this simple example (which could arise e.g. as a copy-paste error) the
``then`` and ``else`` branches are identical and the code is equivalent the
following shorter and cleaner code:

.. code-block:: c++

    test_value(x); // can be omitted unless it has side effects
    y++;
    do_something(x, y);


If this is the intended behavior, then there is no reason to use a conditional
statement; otherwise the issue can be solved by fixing the branch that is
handled incorrectly.

The check also detects repeated branches in longer ``if/else if/else`` chains
where it would be even harder to notice the problem.

In ``switch`` statements the check only reports repeated branches when they are
consecutive, because it is relatively common that the ``case:`` labels have
some natural ordering and rearranging them would decrease the readability of
the code. For example:

.. code-block:: c++

    switch (ch) {
    case 'a':
      return 10;
    case 'A':
      return 10;
    case 'b':
      return 11;
    case 'B':
      return 11;
    default:
      return 10;
    }

Here the check reports that the ``'a'`` and ``'A'`` branches are identical
(and that the ``'b'`` and ``'B'`` branches are also identical), but does not
report that the ``default:`` branch is also identical to the first two branches.
If this is indeed the correct behavior, then it could be implemented as:

.. code-block:: c++

    switch (ch) {
    case 'a':
    case 'A':
      return 10;
    case 'b':
    case 'B':
      return 11;
    default:
      return 10;
    }

Here the check does not warn for the repeated ``return 10;``, which is good if
we want to preserve that ``'a'`` is before ``'b'`` and ``default:`` is the last
branch.

Switch cases marked with the ``[[fallthrough]]`` attribute are ignored.

Finally, the check also examines conditional operators and reports code like:

.. code-block:: c++

    return test_value(x) ? x : x;

Unlike if statements, the check does not detect chains of conditional
operators.

Note: This check also reports situations where branches become identical only
after preprocessing.