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&);
6d5707abdSJohn McCall   operator bool();
7d5707abdSJohn McCall };
8d5707abdSJohn McCall 
9d5707abdSJohn McCall void test() {
10d5707abdSJohn McCall   int x, *p;
11d5707abdSJohn McCall   A a, b;
12d5707abdSJohn McCall 
13d5707abdSJohn McCall   // With scalars.
14fa1e36d0SDouglas Gregor   if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
15*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
16*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
17d5707abdSJohn McCall   if ((x = 7)) {}
18d5707abdSJohn McCall   do {
19fa1e36d0SDouglas Gregor   } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
20*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
21*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
22d5707abdSJohn McCall   do {
23d5707abdSJohn McCall   } while ((x = 7));
24fa1e36d0SDouglas Gregor   while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
25*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
26*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
27*2bf2d3d0SDouglas Gregor 
28d5707abdSJohn McCall   while ((x = 7)) {}
29fa1e36d0SDouglas Gregor   for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
30*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
31*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
32d5707abdSJohn McCall   for (; (x = 7); ) {}
33d5707abdSJohn McCall 
34fa1e36d0SDouglas Gregor   if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
35*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
36*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
37d5707abdSJohn McCall   if ((p = p)) {}
38d5707abdSJohn McCall   do {
39fa1e36d0SDouglas Gregor   } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
40*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
41*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
42d5707abdSJohn McCall   do {
43d5707abdSJohn McCall   } while ((p = p));
44fa1e36d0SDouglas Gregor   while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
45*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
46*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
47d5707abdSJohn McCall   while ((p = p)) {}
48fa1e36d0SDouglas Gregor   for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
49*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
50*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
51d5707abdSJohn McCall   for (; (p = p); ) {}
52d5707abdSJohn McCall 
53d5707abdSJohn McCall   // Initializing variables (shouldn't warn).
54d5707abdSJohn McCall   if (int y = x) {}
55d5707abdSJohn McCall   while (int y = x) {}
56d5707abdSJohn McCall   if (A y = a) {}
57d5707abdSJohn McCall   while (A y = a) {}
58d5707abdSJohn McCall 
59d5707abdSJohn McCall   // With temporaries.
60fa1e36d0SDouglas Gregor   if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
61*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
62*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
63d5707abdSJohn McCall   if ((x = (b+b).foo())) {}
64d5707abdSJohn McCall   do {
65fa1e36d0SDouglas Gregor   } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
66*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
67*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
68d5707abdSJohn McCall   do {
69d5707abdSJohn McCall   } while ((x = (b+b).foo()));
70fa1e36d0SDouglas Gregor   while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
71*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
72*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
73d5707abdSJohn McCall   while ((x = (b+b).foo())) {}
74fa1e36d0SDouglas Gregor   for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
75*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
76*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
77d5707abdSJohn McCall   for (; (x = (b+b).foo()); ) {}
78d5707abdSJohn McCall 
79d5707abdSJohn McCall   // With a user-defined operator.
80fa1e36d0SDouglas Gregor   if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
81*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
82*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
83d5707abdSJohn McCall   if ((a = b + b)) {}
84d5707abdSJohn McCall   do {
85fa1e36d0SDouglas Gregor   } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
86*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
87*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
88d5707abdSJohn McCall   do {
89d5707abdSJohn McCall   } while ((a = b + b));
90fa1e36d0SDouglas Gregor   while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
91*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
92*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
93d5707abdSJohn McCall   while ((a = b + b)) {}
94fa1e36d0SDouglas Gregor   for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
95*2bf2d3d0SDouglas Gregor                 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
96*2bf2d3d0SDouglas Gregor   // expected-note{{place parentheses around the assignment to silence this warning}}
97d5707abdSJohn McCall   for (; (a = b + b); ) {}
98d5707abdSJohn McCall }
99