18fbe78f6SDaniel Dunbar // RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s
2d5707abdSJohn McCall 
3d5707abdSJohn McCall struct A {
4d5707abdSJohn McCall   int foo();
5d5707abdSJohn McCall   friend A operator+(const A&, const A&);
62d4f64f4SDouglas Gregor   A operator|=(const A&);
7d5707abdSJohn McCall   operator bool();
8d5707abdSJohn McCall };
9d5707abdSJohn McCall 
test()10d5707abdSJohn McCall void test() {
11d5707abdSJohn McCall   int x, *p;
12d5707abdSJohn McCall   A a, b;
13d5707abdSJohn McCall 
14d5707abdSJohn McCall   // With scalars.
15fa1e36d0SDouglas Gregor   if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
162bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
172bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
18d5707abdSJohn McCall   if ((x = 7)) {}
19d5707abdSJohn McCall   do {
20fa1e36d0SDouglas Gregor   } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
212bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
222bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
23d5707abdSJohn McCall   do {
24d5707abdSJohn McCall   } while ((x = 7));
25fa1e36d0SDouglas Gregor   while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
262bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
272bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
282bf2d3d0SDouglas Gregor 
29d5707abdSJohn McCall   while ((x = 7)) {}
30fa1e36d0SDouglas Gregor   for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
312bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
322bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
33d5707abdSJohn McCall   for (; (x = 7); ) {}
34d5707abdSJohn McCall 
35fa1e36d0SDouglas Gregor   if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
362bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
372bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
38d5707abdSJohn McCall   if ((p = p)) {}
39d5707abdSJohn McCall   do {
40fa1e36d0SDouglas Gregor   } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
412bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
422bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
43d5707abdSJohn McCall   do {
44d5707abdSJohn McCall   } while ((p = p));
45fa1e36d0SDouglas Gregor   while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
462bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
472bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
48d5707abdSJohn McCall   while ((p = p)) {}
49fa1e36d0SDouglas Gregor   for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
502bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
512bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
52d5707abdSJohn McCall   for (; (p = p); ) {}
53d5707abdSJohn McCall 
54d5707abdSJohn McCall   // Initializing variables (shouldn't warn).
55d5707abdSJohn McCall   if (int y = x) {}
56d5707abdSJohn McCall   while (int y = x) {}
57d5707abdSJohn McCall   if (A y = a) {}
58d5707abdSJohn McCall   while (A y = a) {}
59d5707abdSJohn McCall 
60d5707abdSJohn McCall   // With temporaries.
61fa1e36d0SDouglas Gregor   if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
622bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
632bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
64d5707abdSJohn McCall   if ((x = (b+b).foo())) {}
65d5707abdSJohn McCall   do {
66fa1e36d0SDouglas Gregor   } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
672bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
682bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
69d5707abdSJohn McCall   do {
70d5707abdSJohn McCall   } while ((x = (b+b).foo()));
71fa1e36d0SDouglas Gregor   while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
722bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
732bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
74d5707abdSJohn McCall   while ((x = (b+b).foo())) {}
75fa1e36d0SDouglas Gregor   for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
762bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
772bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
78d5707abdSJohn McCall   for (; (x = (b+b).foo()); ) {}
79d5707abdSJohn McCall 
80d5707abdSJohn McCall   // With a user-defined operator.
81fa1e36d0SDouglas Gregor   if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
822bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
832bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
84d5707abdSJohn McCall   if ((a = b + b)) {}
85d5707abdSJohn McCall   do {
86fa1e36d0SDouglas Gregor   } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
872bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
882bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
89d5707abdSJohn McCall   do {
90d5707abdSJohn McCall   } while ((a = b + b));
91fa1e36d0SDouglas Gregor   while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
922bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
932bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
94d5707abdSJohn McCall   while ((a = b + b)) {}
95fa1e36d0SDouglas Gregor   for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
962bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
972bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
98d5707abdSJohn McCall   for (; (a = b + b); ) {}
992d4f64f4SDouglas Gregor 
1002d4f64f4SDouglas Gregor   // Compound assignments.
1012d4f64f4SDouglas Gregor   if (x |= 2) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
1022d4f64f4SDouglas Gregor                 // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \
1032d4f64f4SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
1042d4f64f4SDouglas Gregor 
1052d4f64f4SDouglas Gregor   if (a |= b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
1062d4f64f4SDouglas Gregor                 // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \
1072d4f64f4SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
1088b6ec687SArgyrios Kyrtzidis 
1098b6ec687SArgyrios Kyrtzidis   if ((x == 5)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
1108b6ec687SArgyrios Kyrtzidis                    // expected-note {{use '=' to turn this equality comparison into an assignment}} \
1118b6ec687SArgyrios Kyrtzidis                    // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
11299c0966fSTed Kremenek 
11399c0966fSTed Kremenek #pragma clang diagnostic push
11499c0966fSTed Kremenek #pragma clang diagnostic ignored "-Wparentheses-equality"
11599c0966fSTed Kremenek   if ((x == 5)) {} // no-warning
11699c0966fSTed Kremenek #pragma clang diagnostic pop
11799c0966fSTed Kremenek 
118582dd685SArgyrios Kyrtzidis   if ((5 == x)) {}
119f4f8278cSArgyrios Kyrtzidis 
120f4f8278cSArgyrios Kyrtzidis #define EQ(x,y) ((x) == (y))
121f4f8278cSArgyrios Kyrtzidis   if (EQ(x, 5)) {}
122f4f8278cSArgyrios Kyrtzidis #undef EQ
123582dd685SArgyrios Kyrtzidis }
124582dd685SArgyrios Kyrtzidis 
125582dd685SArgyrios Kyrtzidis void (*fn)();
126582dd685SArgyrios Kyrtzidis 
test2()127582dd685SArgyrios Kyrtzidis void test2() {
128582dd685SArgyrios Kyrtzidis     if ((fn == test2)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
129582dd685SArgyrios Kyrtzidis                           // expected-note {{use '=' to turn this equality comparison into an assignment}} \
130582dd685SArgyrios Kyrtzidis                           // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
131582dd685SArgyrios Kyrtzidis     if ((test2 == fn)) {}
132d5707abdSJohn McCall }
133c358d9f3STed Kremenek 
134ba699d61SArgyrios Kyrtzidis namespace rdar9027658 {
135ba699d61SArgyrios Kyrtzidis template <typename T>
f(T t)136*db2630fbSRichard Smith void f(T t) {
137*db2630fbSRichard Smith     if ((t.g == 3)) { } // expected-warning {{equality comparison with extraneous parentheses}} \
138ba699d61SArgyrios Kyrtzidis                          // expected-note {{use '=' to turn this equality comparison into an assignment}} \
139ba699d61SArgyrios Kyrtzidis                          // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
140ba699d61SArgyrios Kyrtzidis }
141ba699d61SArgyrios Kyrtzidis 
142ba699d61SArgyrios Kyrtzidis struct S { int g; };
test()143ba699d61SArgyrios Kyrtzidis void test() {
144*db2630fbSRichard Smith   f(S()); // expected-note {{in instantiation}}
145ba699d61SArgyrios Kyrtzidis }
146ba699d61SArgyrios Kyrtzidis }
147