blob: e5a3425804b898ce7df1138e2447845b65184518 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s
John McCall5a881bb2009-10-12 21:59:07 +00002
3struct A {
4 int foo();
5 friend A operator+(const A&, const A&);
6 operator bool();
7};
8
9void test() {
10 int x, *p;
11 A a, b;
12
13 // With scalars.
Douglas Gregor827feec2010-01-08 00:20:23 +000014 if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000015 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
16 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000017 if ((x = 7)) {}
18 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000019 } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000020 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
21 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000022 do {
23 } while ((x = 7));
Douglas Gregor827feec2010-01-08 00:20:23 +000024 while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000025 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
26 // expected-note{{place parentheses around the assignment to silence this warning}}
27
John McCall5a881bb2009-10-12 21:59:07 +000028 while ((x = 7)) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000029 for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000030 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
31 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000032 for (; (x = 7); ) {}
33
Douglas Gregor827feec2010-01-08 00:20:23 +000034 if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000035 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
36 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000037 if ((p = p)) {}
38 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000039 } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000040 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
41 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000042 do {
43 } while ((p = p));
Douglas Gregor827feec2010-01-08 00:20:23 +000044 while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000045 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
46 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000047 while ((p = p)) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000048 for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000049 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
50 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000051 for (; (p = p); ) {}
52
53 // Initializing variables (shouldn't warn).
54 if (int y = x) {}
55 while (int y = x) {}
56 if (A y = a) {}
57 while (A y = a) {}
58
59 // With temporaries.
Douglas Gregor827feec2010-01-08 00:20:23 +000060 if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000061 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
62 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000063 if ((x = (b+b).foo())) {}
64 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000065 } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000066 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
67 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000068 do {
69 } while ((x = (b+b).foo()));
Douglas Gregor827feec2010-01-08 00:20:23 +000070 while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000071 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
72 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000073 while ((x = (b+b).foo())) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000074 for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000075 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
76 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000077 for (; (x = (b+b).foo()); ) {}
78
79 // With a user-defined operator.
Douglas Gregor827feec2010-01-08 00:20:23 +000080 if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000081 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
82 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000083 if ((a = b + b)) {}
84 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000085 } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000086 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
87 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000088 do {
89 } while ((a = b + b));
Douglas Gregor827feec2010-01-08 00:20:23 +000090 while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000091 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
92 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000093 while ((a = b + b)) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000094 for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000095 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
96 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCall5a881bb2009-10-12 21:59:07 +000097 for (; (a = b + b); ) {}
98}