File: redundant-branch-condition.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 (106 lines) | stat: -rw-r--r-- 2,704 bytes parent folder | download | duplicates (18)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
.. title:: clang-tidy - bugprone-redundant-branch-condition

bugprone-redundant-branch-condition
===================================

Finds condition variables in nested ``if`` statements that were also checked in
the outer ``if`` statement and were not changed.

Simple example:

.. code-block:: c

  bool onFire = isBurning();
  if (onFire) {
    if (onFire)
      scream();
  }

Here `onFire` is checked both in the outer ``if`` and the inner ``if`` statement
without a possible change between the two checks. The check warns for this code
and suggests removal of the second checking of variable `onFire`.

The checker also detects redundant condition checks if the condition variable
is an operand of a logical "and" (``&&``) or a logical "or" (``||``) operator:

.. code-block:: c

  bool onFire = isBurning();
  if (onFire) {
    if (onFire && peopleInTheBuilding > 0)
      scream();
  }

.. code-block:: c

  bool onFire = isBurning();
  if (onFire) {
    if (onFire || isCollapsing())
      scream();
  }

In the first case (logical "and") the suggested fix is to remove the redundant
condition variable and keep the other side of the ``&&``. In the second case
(logical "or") the whole ``if`` is removed similarly to the simple case on the
top.

The condition of the outer ``if`` statement may also be a logical "and" (``&&``)
expression:

.. code-block:: c

  bool onFire = isBurning();
  if (onFire && fireFighters < 10) {
    if (someOtherCondition()) {
      if (onFire)
        scream();
    }
  }

The error is also detected if both the outer statement is a logical "and"
(``&&``) and the inner statement is a logical "and" (``&&``) or "or" (``||``).
The inner ``if`` statement does not have to be a direct descendant of the outer
one.

No error is detected if the condition variable may have been changed between the
two checks:

.. code-block:: c

  bool onFire = isBurning();
  if (onFire) {
    tryToExtinguish(onFire);
    if (onFire && peopleInTheBuilding > 0)
      scream();
  }

Every possible change is considered, thus if the condition variable is not
a local variable of the function, it is a volatile or it has an alias (pointer
or reference) then no warning is issued.

Known limitations
^^^^^^^^^^^^^^^^^

The ``else`` branch is not checked currently for negated condition variable:

.. code-block:: c

  bool onFire = isBurning();
  if (onFire) {
    scream();
  } else {
    if (!onFire) {
      continueWork();
    }
  }

The checker currently only detects redundant checking of single condition
variables. More complex expressions are not checked:

.. code-block:: c

  if (peopleInTheBuilding == 1) {
    if (peopleInTheBuilding == 1) {
      doSomething();
    }
  }