1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s 2*67e74705SXin Li 3*67e74705SXin Li struct A { 4*67e74705SXin Li int foo(); 5*67e74705SXin Li friend A operator+(const A&, const A&); 6*67e74705SXin Li A operator|=(const A&); 7*67e74705SXin Li operator bool(); 8*67e74705SXin Li }; 9*67e74705SXin Li test()10*67e74705SXin Livoid test() { 11*67e74705SXin Li int x, *p; 12*67e74705SXin Li A a, b; 13*67e74705SXin Li 14*67e74705SXin Li // With scalars. 15*67e74705SXin Li if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 16*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 17*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 18*67e74705SXin Li if ((x = 7)) {} 19*67e74705SXin Li do { 20*67e74705SXin Li } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 21*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 22*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 23*67e74705SXin Li do { 24*67e74705SXin Li } while ((x = 7)); 25*67e74705SXin Li while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 26*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 27*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 28*67e74705SXin Li 29*67e74705SXin Li while ((x = 7)) {} 30*67e74705SXin Li for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 31*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 32*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 33*67e74705SXin Li for (; (x = 7); ) {} 34*67e74705SXin Li 35*67e74705SXin Li if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 36*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 37*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 38*67e74705SXin Li if ((p = p)) {} 39*67e74705SXin Li do { 40*67e74705SXin Li } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 41*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 42*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 43*67e74705SXin Li do { 44*67e74705SXin Li } while ((p = p)); 45*67e74705SXin Li while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 46*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 47*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 48*67e74705SXin Li while ((p = p)) {} 49*67e74705SXin Li for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 50*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 51*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 52*67e74705SXin Li for (; (p = p); ) {} 53*67e74705SXin Li 54*67e74705SXin Li // Initializing variables (shouldn't warn). 55*67e74705SXin Li if (int y = x) {} 56*67e74705SXin Li while (int y = x) {} 57*67e74705SXin Li if (A y = a) {} 58*67e74705SXin Li while (A y = a) {} 59*67e74705SXin Li 60*67e74705SXin Li // With temporaries. 61*67e74705SXin Li if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 62*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 63*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 64*67e74705SXin Li if ((x = (b+b).foo())) {} 65*67e74705SXin Li do { 66*67e74705SXin Li } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 67*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 68*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 69*67e74705SXin Li do { 70*67e74705SXin Li } while ((x = (b+b).foo())); 71*67e74705SXin Li while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 72*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 73*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 74*67e74705SXin Li while ((x = (b+b).foo())) {} 75*67e74705SXin Li for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 76*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 77*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 78*67e74705SXin Li for (; (x = (b+b).foo()); ) {} 79*67e74705SXin Li 80*67e74705SXin Li // With a user-defined operator. 81*67e74705SXin Li if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 82*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 83*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 84*67e74705SXin Li if ((a = b + b)) {} 85*67e74705SXin Li do { 86*67e74705SXin Li } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 87*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 88*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 89*67e74705SXin Li do { 90*67e74705SXin Li } while ((a = b + b)); 91*67e74705SXin Li while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 92*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 93*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 94*67e74705SXin Li while ((a = b + b)) {} 95*67e74705SXin Li for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 96*67e74705SXin Li // expected-note{{use '==' to turn this assignment into an equality comparison}} \ 97*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 98*67e74705SXin Li for (; (a = b + b); ) {} 99*67e74705SXin Li 100*67e74705SXin Li // Compound assignments. 101*67e74705SXin Li if (x |= 2) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 102*67e74705SXin Li // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \ 103*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 104*67e74705SXin Li 105*67e74705SXin Li if (a |= b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ 106*67e74705SXin Li // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \ 107*67e74705SXin Li // expected-note{{place parentheses around the assignment to silence this warning}} 108*67e74705SXin Li 109*67e74705SXin Li if ((x == 5)) {} // expected-warning {{equality comparison with extraneous parentheses}} \ 110*67e74705SXin Li // expected-note {{use '=' to turn this equality comparison into an assignment}} \ 111*67e74705SXin Li // expected-note {{remove extraneous parentheses around the comparison to silence this warning}} 112*67e74705SXin Li 113*67e74705SXin Li #pragma clang diagnostic push 114*67e74705SXin Li #pragma clang diagnostic ignored "-Wparentheses-equality" 115*67e74705SXin Li if ((x == 5)) {} // no-warning 116*67e74705SXin Li #pragma clang diagnostic pop 117*67e74705SXin Li 118*67e74705SXin Li if ((5 == x)) {} 119*67e74705SXin Li 120*67e74705SXin Li #define EQ(x,y) ((x) == (y)) 121*67e74705SXin Li if (EQ(x, 5)) {} 122*67e74705SXin Li #undef EQ 123*67e74705SXin Li } 124*67e74705SXin Li 125*67e74705SXin Li void (*fn)(); 126*67e74705SXin Li test2()127*67e74705SXin Livoid test2() { 128*67e74705SXin Li if ((fn == test2)) {} // expected-warning {{equality comparison with extraneous parentheses}} \ 129*67e74705SXin Li // expected-note {{use '=' to turn this equality comparison into an assignment}} \ 130*67e74705SXin Li // expected-note {{remove extraneous parentheses around the comparison to silence this warning}} 131*67e74705SXin Li if ((test2 == fn)) {} 132*67e74705SXin Li } 133*67e74705SXin Li 134*67e74705SXin Li namespace rdar9027658 { 135*67e74705SXin Li template <typename T> f(T t)136*67e74705SXin Livoid f(T t) { 137*67e74705SXin Li if ((t.g == 3)) { } // expected-warning {{equality comparison with extraneous parentheses}} \ 138*67e74705SXin Li // expected-note {{use '=' to turn this equality comparison into an assignment}} \ 139*67e74705SXin Li // expected-note {{remove extraneous parentheses around the comparison to silence this warning}} 140*67e74705SXin Li } 141*67e74705SXin Li 142*67e74705SXin Li struct S { int g; }; test()143*67e74705SXin Livoid test() { 144*67e74705SXin Li f(S()); // expected-note {{in instantiation}} 145*67e74705SXin Li } 146*67e74705SXin Li } 147