18fbe78f6SDaniel Dunbar // RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s 2d5707abdSJohn McCall 3d5707abdSJohn McCall struct A { 4d5707abdSJohn McCall int foo(); 5d5707abdSJohn McCall friend A operator+(const A&, const A&); 6d5707abdSJohn McCall operator bool(); 7d5707abdSJohn McCall }; 8d5707abdSJohn McCall 9d5707abdSJohn McCall void test() { 10d5707abdSJohn McCall int x, *p; 11d5707abdSJohn McCall A a, b; 12d5707abdSJohn McCall 13d5707abdSJohn McCall // With scalars. 14*fa1e36d0SDouglas Gregor if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 15*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 16d5707abdSJohn McCall if ((x = 7)) {} 17d5707abdSJohn McCall do { 18*fa1e36d0SDouglas Gregor } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 19*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 20d5707abdSJohn McCall do { 21d5707abdSJohn McCall } while ((x = 7)); 22*fa1e36d0SDouglas Gregor while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 23*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 24d5707abdSJohn McCall while ((x = 7)) {} 25*fa1e36d0SDouglas Gregor for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 26*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 27d5707abdSJohn McCall for (; (x = 7); ) {} 28d5707abdSJohn McCall 29*fa1e36d0SDouglas Gregor if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 30*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 31d5707abdSJohn McCall if ((p = p)) {} 32d5707abdSJohn McCall do { 33*fa1e36d0SDouglas Gregor } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 34*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 35d5707abdSJohn McCall do { 36d5707abdSJohn McCall } while ((p = p)); 37*fa1e36d0SDouglas Gregor while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 38*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 39d5707abdSJohn McCall while ((p = p)) {} 40*fa1e36d0SDouglas Gregor for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 41*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 42d5707abdSJohn McCall for (; (p = p); ) {} 43d5707abdSJohn McCall 44d5707abdSJohn McCall // Initializing variables (shouldn't warn). 45d5707abdSJohn McCall if (int y = x) {} 46d5707abdSJohn McCall while (int y = x) {} 47d5707abdSJohn McCall if (A y = a) {} 48d5707abdSJohn McCall while (A y = a) {} 49d5707abdSJohn McCall 50d5707abdSJohn McCall // With temporaries. 51*fa1e36d0SDouglas Gregor if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 52*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 53d5707abdSJohn McCall if ((x = (b+b).foo())) {} 54d5707abdSJohn McCall do { 55*fa1e36d0SDouglas Gregor } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 56*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 57d5707abdSJohn McCall do { 58d5707abdSJohn McCall } while ((x = (b+b).foo())); 59*fa1e36d0SDouglas Gregor while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 60*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 61d5707abdSJohn McCall while ((x = (b+b).foo())) {} 62*fa1e36d0SDouglas Gregor for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 63*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 64d5707abdSJohn McCall for (; (x = (b+b).foo()); ) {} 65d5707abdSJohn McCall 66d5707abdSJohn McCall // With a user-defined operator. 67*fa1e36d0SDouglas Gregor if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 68*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 69d5707abdSJohn McCall if ((a = b + b)) {} 70d5707abdSJohn McCall do { 71*fa1e36d0SDouglas Gregor } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 72*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 73d5707abdSJohn McCall do { 74d5707abdSJohn McCall } while ((a = b + b)); 75*fa1e36d0SDouglas Gregor while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 76*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 77d5707abdSJohn McCall while ((a = b + b)) {} 78*fa1e36d0SDouglas Gregor for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 79*fa1e36d0SDouglas Gregor // expected-note{{use '==' to turn this assignment into an equality comparison}} 80d5707abdSJohn McCall for (; (a = b + b); ) {} 81d5707abdSJohn McCall } 82