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}} \ |
| 15 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 16 | if ((x = 7)) {} |
| 17 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 18 | } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 19 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 20 | do { |
| 21 | } while ((x = 7)); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 22 | while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 23 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 24 | while ((x = 7)) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 25 | for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 26 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 27 | for (; (x = 7); ) {} |
| 28 | |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 29 | if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 30 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 31 | if ((p = p)) {} |
| 32 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 33 | } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 34 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 35 | do { |
| 36 | } while ((p = p)); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 37 | while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 38 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 39 | while ((p = p)) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 40 | for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 41 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 42 | for (; (p = p); ) {} |
| 43 | |
| 44 | // Initializing variables (shouldn't warn). |
| 45 | if (int y = x) {} |
| 46 | while (int y = x) {} |
| 47 | if (A y = a) {} |
| 48 | while (A y = a) {} |
| 49 | |
| 50 | // With temporaries. |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 51 | if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 52 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 53 | if ((x = (b+b).foo())) {} |
| 54 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 55 | } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 56 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 57 | do { |
| 58 | } while ((x = (b+b).foo())); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 59 | while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 60 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 61 | while ((x = (b+b).foo())) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 62 | for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 63 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 64 | for (; (x = (b+b).foo()); ) {} |
| 65 | |
| 66 | // With a user-defined operator. |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 67 | if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 68 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 69 | if ((a = b + b)) {} |
| 70 | do { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 71 | } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 72 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 73 | do { |
| 74 | } while ((a = b + b)); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 75 | while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 76 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 77 | while ((a = b + b)) {} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame^] | 78 | for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ |
| 79 | // expected-note{{use '==' to turn this assignment into an equality comparison}} |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 80 | for (; (a = b + b); ) {} |
| 81 | } |