74 lines · plain
1.. title:: clang-tidy - bugprone-chained-comparison2 3bugprone-chained-comparison4===========================5 6Check detects chained comparison operators that can lead to unintended7behavior or logical errors.8 9Chained comparisons are expressions that use multiple comparison operators10to compare three or more values. For example, the expression ``a < b < c``11compares the values of ``a``, ``b``, and ``c``. However, this expression does12not evaluate as ``(a < b) && (b < c)``, which is probably what the developer13intended. Instead, it evaluates as ``(a < b) < c``, which may produce14unintended results, especially when the types of ``a``, ``b``, and ``c`` are15different.16 17To avoid such errors, the check will issue a warning when a chained18comparison operator is detected, suggesting to use parentheses to specify19the order of evaluation or to use a logical operator to separate comparison20expressions.21 22Consider 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 executed29 }30 31 32In this example, the developer intended to check if ``a`` is less than ``b``33and ``b`` is less than ``c``. However, the expression ``a < b < c`` is34equivalent to ``(a < b) < c``. Since ``a < b`` is ``true``, the expression35``(a < b) < c`` is evaluated as ``1 < c``, which is equivalent to ``true < c``36and is invalid in this case as ``b < c`` is ``false``.37 38Even that above issue could be detected as comparison of ``int`` to ``bool``,39there 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 executed46 }47 48In this example, the developer intended to check if ``a``, ``b``, and ``c`` are49all equal. However, the expression ``a == b == c`` is evaluated as50``(a == b) == c``. Since ``a == b`` is true, the expression ``(a == b) == c``51is evaluated as ``true == c``, which is equivalent to ``true == true``.52This comparison yields ``true``, even though ``a`` and ``b`` are ``false``, and53are not equal to ``c``.54 55To avoid this issue, the developer can use a logical operator to separate the56comparison expressions, like this:57 58.. code-block:: c++59 60 if (a == b && b == c) {61 // This block will not be executed62 }63 64 65Alternatively, use of parentheses in the comparison expressions can make the66developer's intention more explicit and help avoid misunderstanding.67 68.. code-block:: c++69 70 if ((a == b) == c) {71 // This block will be executed72 }73 74