opnsense-src/test/SemaCXX/warn-assignment-condition.cpp

82 lines
4.2 KiB
C++
Raw Normal View History

2010-01-01 05:34:51 -05:00
// RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s
2009-10-14 14:03:49 -04:00
struct A {
int foo();
friend A operator+(const A&, const A&);
operator bool();
};
void test() {
int x, *p;
A a, b;
// With scalars.
2010-01-15 10:39:40 -05:00
if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
if ((x = 7)) {}
do {
2010-01-15 10:39:40 -05:00
} while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
do {
} while ((x = 7));
2010-01-15 10:39:40 -05:00
while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
while ((x = 7)) {}
2010-01-15 10:39:40 -05:00
for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
for (; (x = 7); ) {}
2010-01-15 10:39:40 -05:00
if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
if ((p = p)) {}
do {
2010-01-15 10:39:40 -05:00
} while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
do {
} while ((p = p));
2010-01-15 10:39:40 -05:00
while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
while ((p = p)) {}
2010-01-15 10:39:40 -05:00
for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04: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-15 10:39:40 -05:00
if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
if ((x = (b+b).foo())) {}
do {
2010-01-15 10:39:40 -05:00
} while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
do {
} while ((x = (b+b).foo()));
2010-01-15 10:39:40 -05:00
while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
while ((x = (b+b).foo())) {}
2010-01-15 10:39:40 -05:00
for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
for (; (x = (b+b).foo()); ) {}
// With a user-defined operator.
2010-01-15 10:39:40 -05:00
if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
if ((a = b + b)) {}
do {
2010-01-15 10:39:40 -05:00
} while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
do {
} while ((a = b + b));
2010-01-15 10:39:40 -05:00
while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
while ((a = b + b)) {}
2010-01-15 10:39:40 -05:00
for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note{{use '==' to turn this assignment into an equality comparison}}
2009-10-14 14:03:49 -04:00
for (; (a = b + b); ) {}
}