| 1 |
.. title:: clang-tidy - bugprone-chained-comparison |
| 2 |
|
| 3 |
bugprone-chained-comparison |
| 4 |
=========================== |
| 5 |
|
| 6 |
Check detects chained comparison operators that can lead to unintended |
| 7 |
behavior or logical errors. |
| 8 |
|
| 9 |
Chained comparisons are expressions that use multiple comparison operators |
| 10 |
to compare three or more values. For example, the expression ``a < b < c`` |
| 11 |
compares the values of ``a``, ``b``, and ``c``. However, this expression does |
| 12 |
not evaluate as ``(a < b) && (b < c)``, which is probably what the developer |
| 13 |
intended. Instead, it evaluates as ``(a < b) < c``, which may produce |
| 14 |
unintended results, especially when the types of ``a``, ``b``, and ``c`` are |
| 15 |
different. |
| 16 |
|
| 17 |
To avoid such errors, the check will issue a warning when a chained |
| 18 |
comparison operator is detected, suggesting to use parentheses to specify |
| 19 |
the order of evaluation or to use a logical operator to separate comparison |
| 20 |
expressions. |
| 21 |
|
| 22 |
Consider the following examples: |
| 23 |
|
| 24 |
.. code-block:: c++ |
| 25 |
|
| 26 |
int a = 2, b = 6, c = 4; |
| 27 |
if (a < b < c) { |
| 28 |
// This block will be executed |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
In this example, the developer intended to check if ``a`` is less than ``b`` |
| 33 |
and ``b`` is less than ``c``. However, the expression ``a < b < c`` is |
| 34 |
equivalent to ``(a < b) < c``. Since ``a < b`` is ``true``, the expression |
| 35 |
``(a < b) < c`` is evaluated as ``1 < c``, which is equivalent to ``true < c`` |
| 36 |
and is invalid in this case as ``b < c`` is ``false``. |
| 37 |
|
| 38 |
Even that above issue could be detected as comparison of ``int`` to ``bool``, |
| 39 |
there is more dangerous example: |
| 40 |
|
| 41 |
.. code-block:: c++ |
| 42 |
|
| 43 |
bool a = false, b = false, c = true; |
| 44 |
if (a == b == c) { |
| 45 |
// This block will be executed |
| 46 |
} |
| 47 |
|
| 48 |
In this example, the developer intended to check if ``a``, ``b``, and ``c`` are |
| 49 |
all equal. However, the expression ``a == b == c`` is evaluated as |
| 50 |
``(a == b) == c``. Since ``a == b`` is true, the expression ``(a == b) == c`` |
| 51 |
is evaluated as ``true == c``, which is equivalent to ``true == true``. |
| 52 |
This comparison yields ``true``, even though ``a`` and ``b`` are ``false``, and |
| 53 |
are not equal to ``c``. |
| 54 |
|
| 55 |
To avoid this issue, the developer can use a logical operator to separate the |
| 56 |
comparison expressions, like this: |
| 57 |
|
| 58 |
.. code-block:: c++ |
| 59 |
|
| 60 |
if (a == b && b == c) { |
| 61 |
// This block will not be executed |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
Alternatively, use of parentheses in the comparison expressions can make the |
| 66 |
developer's intention more explicit and help avoid misunderstanding. |
| 67 |
|
| 68 |
.. code-block:: c++ |
| 69 |
|
| 70 |
if ((a == b) == c) { |
| 71 |
// This block will be executed |
| 72 |
} |
| 73 |
|