blob: c0ef35b252d85d5fc3160955f82a01276b07956c [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&);
Douglas Gregor92c3a042011-01-19 16:50:08 +00006 A operator|=(const A&);
John McCall5a881bb2009-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 Gregor827feec2010-01-08 00:20:23 +000015 if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000018 if ((x = 7)) {}
19 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000020 } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000023 do {
24 } while ((x = 7));
Douglas Gregor827feec2010-01-08 00:20:23 +000025 while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000029 while ((x = 7)) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000030 for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000033 for (; (x = 7); ) {}
34
Douglas Gregor827feec2010-01-08 00:20:23 +000035 if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000038 if ((p = p)) {}
39 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000040 } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000043 do {
44 } while ((p = p));
Douglas Gregor827feec2010-01-08 00:20:23 +000045 while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000048 while ((p = p)) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000049 for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-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 Gregor827feec2010-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 Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000064 if ((x = (b+b).foo())) {}
65 do {
Douglas Gregor827feec2010-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 Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000069 do {
70 } while ((x = (b+b).foo()));
Douglas Gregor827feec2010-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 Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000074 while ((x = (b+b).foo())) {}
Douglas Gregor827feec2010-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 Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000078 for (; (x = (b+b).foo()); ) {}
79
80 // With a user-defined operator.
Douglas Gregor827feec2010-01-08 00:20:23 +000081 if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000084 if ((a = b + b)) {}
85 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000086 } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000089 do {
90 } while ((a = b + b));
Douglas Gregor827feec2010-01-08 00:20:23 +000091 while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000094 while ((a = b + b)) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000095 for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
Douglas Gregor55b38842010-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 McCall5a881bb2009-10-12 21:59:07 +000098 for (; (a = b + b); ) {}
Douglas Gregor92c3a042011-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 Kyrtzidis0e2dc3a2011-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}}
Argyrios Kyrtzidis70f23302011-02-01 19:32:59 +0000112 if ((5 == x)) {}
Argyrios Kyrtzidiscf1620a2011-02-01 22:23:56 +0000113
114#define EQ(x,y) ((x) == (y))
115 if (EQ(x, 5)) {}
116#undef EQ
Argyrios Kyrtzidis70f23302011-02-01 19:32:59 +0000117}
118
119void (*fn)();
120
121void test2() {
122 if ((fn == test2)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
123 // expected-note {{use '=' to turn this equality comparison into an assignment}} \
124 // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
125 if ((test2 == fn)) {}
John McCall5a881bb2009-10-12 21:59:07 +0000126}
Ted Kremenek006ae382011-02-01 22:36:09 +0000127
Argyrios Kyrtzidis170a6a22011-03-28 23:52:04 +0000128namespace rdar9027658 {
129template <typename T>
130void f() {
131 if ((T::g == 3)) { } // expected-warning {{equality comparison with extraneous parentheses}} \
132 // expected-note {{use '=' to turn this equality comparison into an assignment}} \
133 // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
134}
135
136struct S { int g; };
137void test() {
138 f<S>(); // expected-note {{in instantiation}}
139}
140}