1 .. title:: clang-tidy - bugprone-redundant-branch-condition
3 bugprone-redundant-branch-condition
4 ===================================
6 Finds condition variables in nested ``if`` statements that were also checked in
7 the outer ``if`` statement and were not changed.
13 bool onFire = isBurning();
19 Here `onFire` is checked both in the outer ``if`` and the inner ``if`` statement
20 without a possible change between the two checks. The check warns for this code
21 and suggests removal of the second checking of variable `onFire`.
23 The checker also detects redundant condition checks if the condition variable
24 is an operand of a logical "and" (``&&``) or a logical "or" (``||``) operator:
28 bool onFire = isBurning();
30 if (onFire && peopleInTheBuilding > 0)
36 bool onFire = isBurning();
38 if (onFire || isCollapsing())
42 In the first case (logical "and") the suggested fix is to remove the redundant
43 condition variable and keep the other side of the ``&&``. In the second case
44 (logical "or") the whole ``if`` is removed similarly to the simple case on the
47 The condition of the outer ``if`` statement may also be a logical "and" (``&&``)
52 bool onFire = isBurning();
53 if (onFire && fireFighters < 10) {
54 if (someOtherCondition()) {
60 The error is also detected if both the outer statement is a logical "and"
61 (``&&``) and the inner statement is a logical "and" (``&&``) or "or" (``||``).
62 The inner ``if`` statement does not have to be a direct descendant of the outer
65 No error is detected if the condition variable may have been changed between the
70 bool onFire = isBurning();
72 tryToExtinguish(onFire);
73 if (onFire && peopleInTheBuilding > 0)
77 Every possible change is considered, thus if the condition variable is not
78 a local variable of the function, it is a volatile or it has an alias (pointer
79 or reference) then no warning is issued.
84 The ``else`` branch is not checked currently for negated condition variable:
88 bool onFire = isBurning();
97 The checker currently only detects redundant checking of single condition
98 variables. More complex expressions are not checked:
102 if (peopleInTheBuilding == 1) {
103 if (peopleInTheBuilding == 1) {