xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/chained-comparison.c (revision 06c3c3b67cb0287856145806cfb0179def3214bd)
1*06c3c3b6SPiotr Zegar // RUN: %check_clang_tidy %s bugprone-chained-comparison %t
2*06c3c3b6SPiotr Zegar 
badly_chained_1(int x,int y,int z)3*06c3c3b6SPiotr Zegar void badly_chained_1(int x, int y, int z)
4*06c3c3b6SPiotr Zegar {
5*06c3c3b6SPiotr Zegar     int result = x < y < z;
6*06c3c3b6SPiotr Zegar }
7*06c3c3b6SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-2]]:18: 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*06c3c3b6SPiotr Zegar 
badly_chained_2(int x,int y,int z)9*06c3c3b6SPiotr Zegar void badly_chained_2(int x, int y, int z)
10*06c3c3b6SPiotr Zegar {
11*06c3c3b6SPiotr Zegar     int result = x <= y <= z;
12*06c3c3b6SPiotr Zegar }
13*06c3c3b6SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-2]]:18: 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*06c3c3b6SPiotr Zegar 
badly_chained_3(int x,int y,int z)15*06c3c3b6SPiotr Zegar void badly_chained_3(int x, int y, int z)
16*06c3c3b6SPiotr Zegar {
17*06c3c3b6SPiotr Zegar     int result = x > y > z;
18*06c3c3b6SPiotr Zegar }
19*06c3c3b6SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-2]]:18: 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*06c3c3b6SPiotr Zegar 
badly_chained_4(int x,int y,int z)21*06c3c3b6SPiotr Zegar void badly_chained_4(int x, int y, int z)
22*06c3c3b6SPiotr Zegar {
23*06c3c3b6SPiotr Zegar     int result = x >= y >= z;
24*06c3c3b6SPiotr Zegar }
25*06c3c3b6SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-2]]:18: 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*06c3c3b6SPiotr Zegar 
badly_chained_5(int x,int y,int z)27*06c3c3b6SPiotr Zegar void badly_chained_5(int x, int y, int z)
28*06c3c3b6SPiotr Zegar {
29*06c3c3b6SPiotr Zegar     int result = x == y != z;
30*06c3c3b6SPiotr Zegar }
31*06c3c3b6SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-2]]:18: 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*06c3c3b6SPiotr Zegar 
badly_chained_6(int x,int y,int z)33*06c3c3b6SPiotr Zegar void badly_chained_6(int x, int y, int z)
34*06c3c3b6SPiotr Zegar {
35*06c3c3b6SPiotr Zegar     int result = x != y == z;
36*06c3c3b6SPiotr Zegar }
37*06c3c3b6SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-2]]:18: 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*06c3c3b6SPiotr Zegar 
badly_chained_multiple(int a,int b,int c,int d,int e,int f,int g,int h)39*06c3c3b6SPiotr Zegar void badly_chained_multiple(int a, int b, int c, int d, int e, int f, int g, int h)
40*06c3c3b6SPiotr Zegar {
41*06c3c3b6SPiotr Zegar     int result = a == b == c == d == e == f == g == h;
42*06c3c3b6SPiotr Zegar }
43*06c3c3b6SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-2]]:18: 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*06c3c3b6SPiotr Zegar 
badly_chained_limit(int v[29])45*06c3c3b6SPiotr Zegar void badly_chained_limit(int v[29])
46*06c3c3b6SPiotr Zegar {
47*06c3c3b6SPiotr Zegar // CHECK-MESSAGES: :[[@LINE+1]]:18: 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*06c3c3b6SPiotr Zegar     int result = v[0] == v[1] == v[2] == v[3] == v[4] == v[5] == v[6] == v[7] ==
49*06c3c3b6SPiotr Zegar                   v[8] == v[9] == v[10] == v[11] == v[12] == v[13] == v[14] ==
50*06c3c3b6SPiotr Zegar                   v[15] == v[16] == v[17] == v[18] == v[19] == v[20] == v[21] ==
51*06c3c3b6SPiotr Zegar                   v[22] == v[23] == v[24] == v[25] == v[26] == v[27] == v[28];
52*06c3c3b6SPiotr Zegar 
53*06c3c3b6SPiotr Zegar }
54*06c3c3b6SPiotr Zegar 
badly_chained_parens2(int x,int y,int z,int t,int a,int b)55*06c3c3b6SPiotr Zegar void badly_chained_parens2(int x, int y, int z, int t, int a, int b)
56*06c3c3b6SPiotr Zegar {
57*06c3c3b6SPiotr Zegar     int result = (x < y) < (z && t) > (a == b);
58*06c3c3b6SPiotr Zegar }
59*06c3c3b6SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-2]]:18: 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*06c3c3b6SPiotr Zegar 
badly_chained_inner(int x,int y,int z,int t,int u)61*06c3c3b6SPiotr Zegar void badly_chained_inner(int x, int y, int z, int t, int u)
62*06c3c3b6SPiotr Zegar {
63*06c3c3b6SPiotr Zegar     int result = x && y < z < t && u;
64*06c3c3b6SPiotr Zegar }
65*06c3c3b6SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-2]]:23: 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*06c3c3b6SPiotr Zegar 
properly_chained_1(int x,int y,int z)67*06c3c3b6SPiotr Zegar void properly_chained_1(int x, int y, int z)
68*06c3c3b6SPiotr Zegar {
69*06c3c3b6SPiotr Zegar     int result = x < y && y < z;
70*06c3c3b6SPiotr Zegar }
71*06c3c3b6SPiotr Zegar 
properly_chained_2(int x,int y,int z)72*06c3c3b6SPiotr Zegar void properly_chained_2(int x, int y, int z)
73*06c3c3b6SPiotr Zegar {
74*06c3c3b6SPiotr Zegar     int result = (x < y) == z;
75*06c3c3b6SPiotr Zegar }
76*06c3c3b6SPiotr Zegar 
77