1// RUN: %check_clang_tidy -std=c++98-or-later %s bugprone-chained-comparison %t
2
3void badly_chained_1(int x, int y, int z)
4{
5 bool result = x < y < z;
6}
7// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: chained comparison 'v0 < v1 < v2' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
8
9void badly_chained_2(int x, int y, int z)
10{
11 bool result = x <= y <= z;
12}
13// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: chained comparison 'v0 <= v1 <= v2' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
14
15void badly_chained_3(int x, int y, int z)
16{
17 bool result = x > y > z;
18}
19// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: chained comparison 'v0 > v1 > v2' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
20
21void badly_chained_4(int x, int y, int z)
22{
23 bool result = x >= y >= z;
24}
25// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: chained comparison 'v0 >= v1 >= v2' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
26
27void badly_chained_5(int x, int y, int z)
28{
29 bool result = x == y != z;
30}
31// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: chained comparison 'v0 == v1 != v2' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
32
33void badly_chained_6(bool x, bool y, bool z)
34{
35 bool result = x != y == z;
36}
37// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: chained comparison 'v0 != v1 == v2' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
38
39void badly_chained_multiple(bool a, bool b, bool c, bool d, bool e, bool f, bool g, bool h)
40{
41 bool result = a == b == c == d == e == f == g == h;
42}
43// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: chained comparison 'v0 == v1 == v2 == v3 == v4 == v5 == v6 == v7' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
44
45void badly_chained_limit(bool v[29])
46{
47// CHECK-MESSAGES: :[[@LINE+1]]:19: warning: chained comparison 'v0 == v1 == v2 == v3 == v4 == v5 == v6 == v7 == v8 == v9 == v10 == v11 == v12 == v13 == v14 == v15 == v16 == v17 == v18 == v19 == v20 == v21 == v22 == v23 == v24 == v25 == v26 == v27 == v28' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
48 bool result = v[0] == v[1] == v[2] == v[3] == v[4] == v[5] == v[6] == v[7] ==
49 v[8] == v[9] == v[10] == v[11] == v[12] == v[13] == v[14] ==
50 v[15] == v[16] == v[17] == v[18] == v[19] == v[20] == v[21] ==
51 v[22] == v[23] == v[24] == v[25] == v[26] == v[27] == v[28];
52
53}
54
55void badly_chained_parens2(int x, int y, int z, int t, int a, int b)
56{
57 bool result = (x < y) < (z && t) > (a == b);
58}
59// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: chained comparison 'v0 < v1 > v2' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
60
61void badly_chained_inner(int x, int y, int z, int t, int u)
62{
63 bool result = x && y < z < t && u;
64}
65// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: chained comparison 'v0 < v1 < v2' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
66
67void properly_chained_1(int x, int y, int z)
68{
69 bool result = x < y && y < z;
70}
71
72void properly_chained_2(int x, int y, bool z)
73{
74 bool result = (x < y) == z;
75}
76
77struct Value {
78 bool operator<(const Value&) const;
79};
80
81bool operator==(bool, const Value&);
82
83bool badWithCppOperator(Value a, Value b, Value c) {
84 return a < b == c;
85}
86// CHECK-MESSAGES: :[[@LINE-2]]:12: warning: chained comparison 'v0 < v1 == v2' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
87
88bool mixedBinaryAndCpp(Value a, Value b, bool c) {
89 return a < b == c;
90}
91// CHECK-MESSAGES: :[[@LINE-2]]:12: warning: chained comparison 'v0 < v1 == v2' may generate unintended results, use parentheses to specify order of evaluation or a logical operator to separate comparison expressions [bugprone-chained-comparison]
92

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/chained-comparison.cpp