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&); |
| 6 | operator bool(); |
| 7 | }; |
| 8 | |
| 9 | void test() { |
| 10 | int x, *p; |
| 11 | A a, b; |
| 12 | |
| 13 | // With scalars. |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 14 | 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] | 15 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 16 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 17 | if ((x = 7)) {} |
| 18 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 19 | } 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] | 20 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 21 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 22 | do { |
| 23 | } while ((x = 7)); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 24 | 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] | 25 | // 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 McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 28 | while ((x = 7)) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 29 | 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] | 30 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 31 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 32 | for (; (x = 7); ) {} |
| 33 | |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 34 | 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] | 35 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 36 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 37 | if ((p = p)) {} |
| 38 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 39 | } 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] | 40 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 41 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 42 | do { |
| 43 | } while ((p = p)); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 44 | 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] | 45 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 46 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 47 | while ((p = p)) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 48 | 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] | 49 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 50 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 51 | 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 Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 60 | 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] | 61 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 62 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 63 | if ((x = (b+b).foo())) {} |
| 64 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 65 | } 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] | 66 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 67 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 68 | do { |
| 69 | } while ((x = (b+b).foo())); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 70 | 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] | 71 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 72 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 73 | while ((x = (b+b).foo())) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 74 | 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] | 75 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 76 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 77 | for (; (x = (b+b).foo()); ) {} |
| 78 | |
| 79 | // With a user-defined operator. |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 80 | 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] | 81 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 82 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 83 | if ((a = b + b)) {} |
| 84 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 85 | } 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] | 86 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 87 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 88 | do { |
| 89 | } while ((a = b + b)); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 90 | 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] | 91 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 92 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 93 | while ((a = b + b)) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 94 | 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] | 95 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 96 | // expected-note{{place parentheses around the assignment to silence this warning}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 97 | for (; (a = b + b); ) {} |
| 98 | } |