1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc struct A {
4*f4a2713aSLionel Sambuc int foo();
5*f4a2713aSLionel Sambuc friend A operator+(const A&, const A&);
6*f4a2713aSLionel Sambuc A operator|=(const A&);
7*f4a2713aSLionel Sambuc operator bool();
8*f4a2713aSLionel Sambuc };
9*f4a2713aSLionel Sambuc
test()10*f4a2713aSLionel Sambuc void test() {
11*f4a2713aSLionel Sambuc int x, *p;
12*f4a2713aSLionel Sambuc A a, b;
13*f4a2713aSLionel Sambuc
14*f4a2713aSLionel Sambuc // With scalars.
15*f4a2713aSLionel Sambuc if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
16*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
17*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
18*f4a2713aSLionel Sambuc if ((x = 7)) {}
19*f4a2713aSLionel Sambuc do {
20*f4a2713aSLionel Sambuc } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
21*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
22*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
23*f4a2713aSLionel Sambuc do {
24*f4a2713aSLionel Sambuc } while ((x = 7));
25*f4a2713aSLionel Sambuc while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
26*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
27*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
28*f4a2713aSLionel Sambuc
29*f4a2713aSLionel Sambuc while ((x = 7)) {}
30*f4a2713aSLionel Sambuc for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
31*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
32*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
33*f4a2713aSLionel Sambuc for (; (x = 7); ) {}
34*f4a2713aSLionel Sambuc
35*f4a2713aSLionel Sambuc if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
36*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
37*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
38*f4a2713aSLionel Sambuc if ((p = p)) {}
39*f4a2713aSLionel Sambuc do {
40*f4a2713aSLionel Sambuc } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
41*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
42*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
43*f4a2713aSLionel Sambuc do {
44*f4a2713aSLionel Sambuc } while ((p = p));
45*f4a2713aSLionel Sambuc while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
46*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
47*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
48*f4a2713aSLionel Sambuc while ((p = p)) {}
49*f4a2713aSLionel Sambuc for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
50*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
51*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
52*f4a2713aSLionel Sambuc for (; (p = p); ) {}
53*f4a2713aSLionel Sambuc
54*f4a2713aSLionel Sambuc // Initializing variables (shouldn't warn).
55*f4a2713aSLionel Sambuc if (int y = x) {}
56*f4a2713aSLionel Sambuc while (int y = x) {}
57*f4a2713aSLionel Sambuc if (A y = a) {}
58*f4a2713aSLionel Sambuc while (A y = a) {}
59*f4a2713aSLionel Sambuc
60*f4a2713aSLionel Sambuc // With temporaries.
61*f4a2713aSLionel Sambuc if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
62*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
63*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
64*f4a2713aSLionel Sambuc if ((x = (b+b).foo())) {}
65*f4a2713aSLionel Sambuc do {
66*f4a2713aSLionel Sambuc } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
67*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
68*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
69*f4a2713aSLionel Sambuc do {
70*f4a2713aSLionel Sambuc } while ((x = (b+b).foo()));
71*f4a2713aSLionel Sambuc while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
72*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
73*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
74*f4a2713aSLionel Sambuc while ((x = (b+b).foo())) {}
75*f4a2713aSLionel Sambuc for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
76*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
77*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
78*f4a2713aSLionel Sambuc for (; (x = (b+b).foo()); ) {}
79*f4a2713aSLionel Sambuc
80*f4a2713aSLionel Sambuc // With a user-defined operator.
81*f4a2713aSLionel Sambuc if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
82*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
83*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
84*f4a2713aSLionel Sambuc if ((a = b + b)) {}
85*f4a2713aSLionel Sambuc do {
86*f4a2713aSLionel Sambuc } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
87*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
88*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
89*f4a2713aSLionel Sambuc do {
90*f4a2713aSLionel Sambuc } while ((a = b + b));
91*f4a2713aSLionel Sambuc while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
92*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
93*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
94*f4a2713aSLionel Sambuc while ((a = b + b)) {}
95*f4a2713aSLionel Sambuc for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
96*f4a2713aSLionel Sambuc // expected-note{{use '==' to turn this assignment into an equality comparison}} \
97*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
98*f4a2713aSLionel Sambuc for (; (a = b + b); ) {}
99*f4a2713aSLionel Sambuc
100*f4a2713aSLionel Sambuc // Compound assignments.
101*f4a2713aSLionel Sambuc if (x |= 2) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
102*f4a2713aSLionel Sambuc // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \
103*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
104*f4a2713aSLionel Sambuc
105*f4a2713aSLionel Sambuc if (a |= b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
106*f4a2713aSLionel Sambuc // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \
107*f4a2713aSLionel Sambuc // expected-note{{place parentheses around the assignment to silence this warning}}
108*f4a2713aSLionel Sambuc
109*f4a2713aSLionel Sambuc if ((x == 5)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
110*f4a2713aSLionel Sambuc // expected-note {{use '=' to turn this equality comparison into an assignment}} \
111*f4a2713aSLionel Sambuc // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
112*f4a2713aSLionel Sambuc
113*f4a2713aSLionel Sambuc #pragma clang diagnostic push
114*f4a2713aSLionel Sambuc #pragma clang diagnostic ignored "-Wparentheses-equality"
115*f4a2713aSLionel Sambuc if ((x == 5)) {} // no-warning
116*f4a2713aSLionel Sambuc #pragma clang diagnostic pop
117*f4a2713aSLionel Sambuc
118*f4a2713aSLionel Sambuc if ((5 == x)) {}
119*f4a2713aSLionel Sambuc
120*f4a2713aSLionel Sambuc #define EQ(x,y) ((x) == (y))
121*f4a2713aSLionel Sambuc if (EQ(x, 5)) {}
122*f4a2713aSLionel Sambuc #undef EQ
123*f4a2713aSLionel Sambuc }
124*f4a2713aSLionel Sambuc
125*f4a2713aSLionel Sambuc void (*fn)();
126*f4a2713aSLionel Sambuc
test2()127*f4a2713aSLionel Sambuc void test2() {
128*f4a2713aSLionel Sambuc if ((fn == test2)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
129*f4a2713aSLionel Sambuc // expected-note {{use '=' to turn this equality comparison into an assignment}} \
130*f4a2713aSLionel Sambuc // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
131*f4a2713aSLionel Sambuc if ((test2 == fn)) {}
132*f4a2713aSLionel Sambuc }
133*f4a2713aSLionel Sambuc
134*f4a2713aSLionel Sambuc namespace rdar9027658 {
135*f4a2713aSLionel Sambuc template <typename T>
f(T t)136*f4a2713aSLionel Sambuc void f(T t) {
137*f4a2713aSLionel Sambuc if ((t.g == 3)) { } // expected-warning {{equality comparison with extraneous parentheses}} \
138*f4a2713aSLionel Sambuc // expected-note {{use '=' to turn this equality comparison into an assignment}} \
139*f4a2713aSLionel Sambuc // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
140*f4a2713aSLionel Sambuc }
141*f4a2713aSLionel Sambuc
142*f4a2713aSLionel Sambuc struct S { int g; };
test()143*f4a2713aSLionel Sambuc void test() {
144*f4a2713aSLionel Sambuc f(S()); // expected-note {{in instantiation}}
145*f4a2713aSLionel Sambuc }
146*f4a2713aSLionel Sambuc }
147