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&);
6*2d4f64f4SDouglas Gregor   A operator|=(const A&);
7d5707abdSJohn McCall   operator bool();
8d5707abdSJohn McCall };
9d5707abdSJohn McCall 
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); ) {}
99*2d4f64f4SDouglas Gregor 
100*2d4f64f4SDouglas Gregor   // Compound assignments.
101*2d4f64f4SDouglas Gregor   if (x |= 2) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
102*2d4f64f4SDouglas Gregor                 // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \
103*2d4f64f4SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
104*2d4f64f4SDouglas Gregor 
105*2d4f64f4SDouglas Gregor   if (a |= b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
106*2d4f64f4SDouglas Gregor                 // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \
107*2d4f64f4SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
108d5707abdSJohn McCall }
109