Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 2 | |
| 3 | struct A { |
| 4 | int foo(); |
| 5 | friend A operator+(const A&, const A&); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 6 | A operator|=(const A&); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7 | operator bool(); |
| 8 | }; |
| 9 | |
| 10 | void test() { |
| 11 | int x, *p; |
| 12 | A a, b; |
| 13 | |
| 14 | // With scalars. |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 15 | if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 16 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 17 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 18 | if ((x = 7)) {} |
| 19 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 20 | } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 21 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 22 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 23 | do { |
| 24 | } while ((x = 7)); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 25 | while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 26 | // 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 McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 29 | while ((x = 7)) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 30 | for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 31 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 32 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 33 | for (; (x = 7); ) {} |
| 34 | |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 35 | if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 36 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 37 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 38 | if ((p = p)) {} |
| 39 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 40 | } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 41 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 42 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 43 | do { |
| 44 | } while ((p = p)); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 45 | while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 46 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 47 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 48 | while ((p = p)) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 49 | for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 50 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 51 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 52 | 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 Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 61 | if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 62 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 63 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 64 | if ((x = (b+b).foo())) {} |
| 65 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 66 | } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 67 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 68 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 69 | do { |
| 70 | } while ((x = (b+b).foo())); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 71 | while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 72 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 73 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 74 | while ((x = (b+b).foo())) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 75 | for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 76 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 77 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 78 | for (; (x = (b+b).foo()); ) {} |
| 79 | |
| 80 | // With a user-defined operator. |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 81 | if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 82 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 83 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 84 | if ((a = b + b)) {} |
| 85 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 86 | } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 87 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 88 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 89 | do { |
| 90 | } while ((a = b + b)); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 91 | while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 92 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 93 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 94 | while ((a = b + b)) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 95 | for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 96 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 97 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 98 | for (; (a = b + b); ) {} |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 99 | |
| 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 Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 108 | |
| 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 Kremenek | f1bb0b0 | 2011-09-06 20:58:32 +0000 | [diff] [blame] | 112 | |
| 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 Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 118 | if ((5 == x)) {} |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 119 | |
| 120 | #define EQ(x,y) ((x) == (y)) |
| 121 | if (EQ(x, 5)) {} |
| 122 | #undef EQ |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void (*fn)(); |
| 126 | |
| 127 | void 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 McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 132 | } |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 133 | |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 134 | namespace rdar9027658 { |
| 135 | template <typename T> |
Richard Smith | efeeccf | 2012-10-21 03:28:35 +0000 | [diff] [blame] | 136 | void f(T t) { |
| 137 | if ((t.g == 3)) { } // expected-warning {{equality comparison with extraneous parentheses}} \ |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 138 | // 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 | |
| 142 | struct S { int g; }; |
| 143 | void test() { |
Richard Smith | efeeccf | 2012-10-21 03:28:35 +0000 | [diff] [blame] | 144 | f(S()); // expected-note {{in instantiation}} |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 145 | } |
| 146 | } |