blob: 09084e36bb49169a71bd3fbd7e2942069f3cfe85 [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s
John McCalld5707ab2009-10-12 21:59:07 +00002
3struct A {
4 int foo();
5 friend A operator+(const A&, const A&);
Douglas Gregor2d4f64f2011-01-19 16:50:08 +00006 A operator|=(const A&);
John McCalld5707ab2009-10-12 21:59:07 +00007 operator bool();
8};
9
10void test() {
11 int x, *p;
12 A a, b;
13
14 // With scalars.
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000015 if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000016 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
17 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000018 if ((x = 7)) {}
19 do {
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000020 } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000021 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
22 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000023 do {
24 } while ((x = 7));
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000025 while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000026 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
27 // expected-note{{place parentheses around the assignment to silence this warning}}
28
John McCalld5707ab2009-10-12 21:59:07 +000029 while ((x = 7)) {}
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000030 for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000031 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
32 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000033 for (; (x = 7); ) {}
34
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000035 if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000036 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
37 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000038 if ((p = p)) {}
39 do {
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000040 } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000041 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
42 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000043 do {
44 } while ((p = p));
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000045 while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000046 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
47 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000048 while ((p = p)) {}
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000049 for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000050 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
51 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000052 for (; (p = p); ) {}
53
54 // Initializing variables (shouldn't warn).
55 if (int y = x) {}
56 while (int y = x) {}
57 if (A y = a) {}
58 while (A y = a) {}
59
60 // With temporaries.
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000061 if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000062 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
63 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000064 if ((x = (b+b).foo())) {}
65 do {
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000066 } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000067 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
68 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000069 do {
70 } while ((x = (b+b).foo()));
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000071 while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000072 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
73 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000074 while ((x = (b+b).foo())) {}
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000075 for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000076 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
77 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000078 for (; (x = (b+b).foo()); ) {}
79
80 // With a user-defined operator.
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000081 if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000082 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
83 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000084 if ((a = b + b)) {}
85 do {
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000086 } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000087 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
88 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000089 do {
90 } while ((a = b + b));
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000091 while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000092 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
93 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000094 while ((a = b + b)) {}
Douglas Gregorfa1e36d2010-01-08 00:20:23 +000095 for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor2bf2d3d2010-04-14 16:09:52 +000096 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
97 // expected-note{{place parentheses around the assignment to silence this warning}}
John McCalld5707ab2009-10-12 21:59:07 +000098 for (; (a = b + b); ) {}
Douglas Gregor2d4f64f2011-01-19 16:50:08 +000099
100 // Compound assignments.
101 if (x |= 2) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
102 // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \
103 // expected-note{{place parentheses around the assignment to silence this warning}}
104
105 if (a |= b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
106 // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \
107 // expected-note{{place parentheses around the assignment to silence this warning}}
Argyrios Kyrtzidis8b6ec682011-02-01 18:24:22 +0000108
109 if ((x == 5)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
110 // expected-note {{use '=' to turn this equality comparison into an assignment}} \
111 // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
Ted Kremenek99c09662011-09-06 20:58:32 +0000112
113#pragma clang diagnostic push
114#pragma clang diagnostic ignored "-Wparentheses-equality"
115 if ((x == 5)) {} // no-warning
116#pragma clang diagnostic pop
117
Argyrios Kyrtzidis582dd682011-02-01 19:32:59 +0000118 if ((5 == x)) {}
Argyrios Kyrtzidisf4f82782011-02-01 22:23:56 +0000119
120#define EQ(x,y) ((x) == (y))
121 if (EQ(x, 5)) {}
122#undef EQ
Argyrios Kyrtzidis582dd682011-02-01 19:32:59 +0000123}
124
125void (*fn)();
126
127void test2() {
128 if ((fn == test2)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
129 // expected-note {{use '=' to turn this equality comparison into an assignment}} \
130 // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
131 if ((test2 == fn)) {}
John McCalld5707ab2009-10-12 21:59:07 +0000132}
Ted Kremenekc358d9f2011-02-01 22:36:09 +0000133
Argyrios Kyrtzidisba699d62011-03-28 23:52:04 +0000134namespace rdar9027658 {
135template <typename T>
Richard Smithdb2630f2012-10-21 03:28:35 +0000136void f(T t) {
137 if ((t.g == 3)) { } // expected-warning {{equality comparison with extraneous parentheses}} \
Argyrios Kyrtzidisba699d62011-03-28 23:52:04 +0000138 // expected-note {{use '=' to turn this equality comparison into an assignment}} \
139 // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
140}
141
142struct S { int g; };
143void test() {
Richard Smithdb2630f2012-10-21 03:28:35 +0000144 f(S()); // expected-note {{in instantiation}}
Argyrios Kyrtzidisba699d62011-03-28 23:52:04 +0000145}
146}