2009-12-16 04:14:24 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s
|
2009-10-13 05:59:07 +08:00
|
|
|
|
|
|
|
struct A {
|
|
|
|
int foo();
|
|
|
|
friend A operator+(const A&, const A&);
|
2011-01-20 00:50:08 +08:00
|
|
|
A operator|=(const A&);
|
2009-10-13 05:59:07 +08:00
|
|
|
operator bool();
|
|
|
|
};
|
|
|
|
|
|
|
|
void test() {
|
|
|
|
int x, *p;
|
|
|
|
A a, b;
|
|
|
|
|
|
|
|
// With scalars.
|
2010-01-08 08:20:23 +08:00
|
|
|
if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
if ((x = 7)) {}
|
|
|
|
do {
|
2010-01-08 08:20:23 +08:00
|
|
|
} while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
do {
|
|
|
|
} while ((x = 7));
|
2010-01-08 08:20:23 +08:00
|
|
|
while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
|
|
|
|
2009-10-13 05:59:07 +08:00
|
|
|
while ((x = 7)) {}
|
2010-01-08 08:20:23 +08:00
|
|
|
for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
for (; (x = 7); ) {}
|
|
|
|
|
2010-01-08 08:20:23 +08:00
|
|
|
if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
if ((p = p)) {}
|
|
|
|
do {
|
2010-01-08 08:20:23 +08:00
|
|
|
} while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
do {
|
|
|
|
} while ((p = p));
|
2010-01-08 08:20:23 +08:00
|
|
|
while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
while ((p = p)) {}
|
2010-01-08 08:20:23 +08:00
|
|
|
for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
for (; (p = p); ) {}
|
|
|
|
|
|
|
|
// Initializing variables (shouldn't warn).
|
|
|
|
if (int y = x) {}
|
|
|
|
while (int y = x) {}
|
|
|
|
if (A y = a) {}
|
|
|
|
while (A y = a) {}
|
|
|
|
|
|
|
|
// With temporaries.
|
2010-01-08 08:20:23 +08:00
|
|
|
if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
if ((x = (b+b).foo())) {}
|
|
|
|
do {
|
2010-01-08 08:20:23 +08:00
|
|
|
} while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
do {
|
|
|
|
} while ((x = (b+b).foo()));
|
2010-01-08 08:20:23 +08:00
|
|
|
while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
while ((x = (b+b).foo())) {}
|
2010-01-08 08:20:23 +08:00
|
|
|
for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
for (; (x = (b+b).foo()); ) {}
|
|
|
|
|
|
|
|
// With a user-defined operator.
|
2010-01-08 08:20:23 +08:00
|
|
|
if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
if ((a = b + b)) {}
|
|
|
|
do {
|
2010-01-08 08:20:23 +08:00
|
|
|
} while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
do {
|
|
|
|
} while ((a = b + b));
|
2010-01-08 08:20:23 +08:00
|
|
|
while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
while ((a = b + b)) {}
|
2010-01-08 08:20:23 +08:00
|
|
|
for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-13 05:59:07 +08:00
|
|
|
for (; (a = b + b); ) {}
|
2011-01-20 00:50:08 +08:00
|
|
|
|
|
|
|
// Compound assignments.
|
|
|
|
if (x |= 2) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
|
|
|
// expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
|
|
|
|
|
|
|
if (a |= b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
|
|
|
|
// expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2011-02-02 02:24:22 +08:00
|
|
|
|
|
|
|
if ((x == 5)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
|
|
|
|
// expected-note {{use '=' to turn this equality comparison into an assignment}} \
|
|
|
|
// expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
|
2011-09-07 04:58:32 +08:00
|
|
|
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wparentheses-equality"
|
|
|
|
if ((x == 5)) {} // no-warning
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
2011-02-02 03:32:59 +08:00
|
|
|
if ((5 == x)) {}
|
2011-02-02 06:23:56 +08:00
|
|
|
|
|
|
|
#define EQ(x,y) ((x) == (y))
|
|
|
|
if (EQ(x, 5)) {}
|
|
|
|
#undef EQ
|
2011-02-02 03:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void (*fn)();
|
|
|
|
|
|
|
|
void test2() {
|
|
|
|
if ((fn == test2)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
|
|
|
|
// expected-note {{use '=' to turn this equality comparison into an assignment}} \
|
|
|
|
// expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
|
|
|
|
if ((test2 == fn)) {}
|
2009-10-13 05:59:07 +08:00
|
|
|
}
|
2011-02-02 06:36:09 +08:00
|
|
|
|
2011-03-29 07:52:04 +08:00
|
|
|
namespace rdar9027658 {
|
|
|
|
template <typename T>
|
2012-10-21 11:28:35 +08:00
|
|
|
void f(T t) {
|
|
|
|
if ((t.g == 3)) { } // expected-warning {{equality comparison with extraneous parentheses}} \
|
2011-03-29 07:52:04 +08:00
|
|
|
// expected-note {{use '=' to turn this equality comparison into an assignment}} \
|
|
|
|
// expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S { int g; };
|
|
|
|
void test() {
|
2012-10-21 11:28:35 +08:00
|
|
|
f(S()); // expected-note {{in instantiation}}
|
2011-03-29 07:52:04 +08:00
|
|
|
}
|
|
|
|
}
|