xref: /llvm-project/clang/test/SemaCXX/warn-unused-comparison.cpp (revision 78ecb8737d97a038770af9f6a1ad1d7925c87eb7)
16e8da6a2SDouglas Gregor // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -Wno-unused -Wunused-comparison %s
2e2669397SChandler Carruth 
3e2669397SChandler Carruth struct A {
4e2669397SChandler Carruth   bool operator==(const A&);
5e2669397SChandler Carruth   bool operator!=(const A&);
699e1c951SRichard Trieu   bool operator<(const A&);
799e1c951SRichard Trieu   bool operator>(const A&);
899e1c951SRichard Trieu   bool operator<=(const A&);
999e1c951SRichard Trieu   bool operator>=(const A&);
10e2669397SChandler Carruth   A operator|=(const A&);
11e2669397SChandler Carruth   operator bool();
12e2669397SChandler Carruth };
13e2669397SChandler Carruth 
test()14e2669397SChandler Carruth void test() {
15e2669397SChandler Carruth   int x, *p;
16e2669397SChandler Carruth   A a, b;
17e2669397SChandler Carruth 
18e2669397SChandler Carruth   x == 7; // expected-warning {{equality comparison result unused}} \
19e2669397SChandler Carruth           // expected-note {{use '=' to turn this equality comparison into an assignment}}
20e2669397SChandler Carruth   x != 7; // expected-warning {{inequality comparison result unused}} \
21e2669397SChandler Carruth           // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
2299e1c951SRichard Trieu   x < 7;  // expected-warning {{relational comparison result unused}}
2399e1c951SRichard Trieu   x > 7;  // expected-warning {{relational comparison result unused}}
2499e1c951SRichard Trieu   x <= 7; // expected-warning {{relational comparison result unused}}
2599e1c951SRichard Trieu   x >= 7; // expected-warning {{relational comparison result unused}}
2699e1c951SRichard Trieu 
27e2669397SChandler Carruth   7 == x; // expected-warning {{equality comparison result unused}}
28e2669397SChandler Carruth   p == p; // expected-warning {{equality comparison result unused}} \
29e2669397SChandler Carruth           // expected-note {{use '=' to turn this equality comparison into an assignment}} \
30e2669397SChandler Carruth           // expected-warning {{self-comparison always evaluates to true}}
3146339475SChandler Carruth   a == a; // expected-warning {{equality comparison result unused}} \
3246339475SChandler Carruth           // expected-note {{use '=' to turn this equality comparison into an assignment}}
3346339475SChandler Carruth   a == b; // expected-warning {{equality comparison result unused}} \
3446339475SChandler Carruth           // expected-note {{use '=' to turn this equality comparison into an assignment}}
3546339475SChandler Carruth   a != b; // expected-warning {{inequality comparison result unused}} \
3646339475SChandler Carruth           // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
3799e1c951SRichard Trieu   a < b;  // expected-warning {{relational comparison result unused}}
3899e1c951SRichard Trieu   a > b;  // expected-warning {{relational comparison result unused}}
3999e1c951SRichard Trieu   a <= b; // expected-warning {{relational comparison result unused}}
4099e1c951SRichard Trieu   a >= b; // expected-warning {{relational comparison result unused}}
4199e1c951SRichard Trieu 
4246339475SChandler Carruth   A() == b; // expected-warning {{equality comparison result unused}}
43e2669397SChandler Carruth   if (42) x == 7; // expected-warning {{equality comparison result unused}} \
44e2669397SChandler Carruth                   // expected-note {{use '=' to turn this equality comparison into an assignment}}
45e2669397SChandler Carruth   else if (42) x == 7; // expected-warning {{equality comparison result unused}} \
46e2669397SChandler Carruth                        // expected-note {{use '=' to turn this equality comparison into an assignment}}
47e2669397SChandler Carruth   else x == 7; // expected-warning {{equality comparison result unused}} \
48e2669397SChandler Carruth                // expected-note {{use '=' to turn this equality comparison into an assignment}}
49e2669397SChandler Carruth   do x == 7; // expected-warning {{equality comparison result unused}} \
50e2669397SChandler Carruth              // expected-note {{use '=' to turn this equality comparison into an assignment}}
51e2669397SChandler Carruth   while (false);
52e2669397SChandler Carruth   while (false) x == 7; // expected-warning {{equality comparison result unused}} \
53e2669397SChandler Carruth                         // expected-note {{use '=' to turn this equality comparison into an assignment}}
54e2669397SChandler Carruth   for (x == 7; // expected-warning {{equality comparison result unused}} \
55e2669397SChandler Carruth                // expected-note {{use '=' to turn this equality comparison into an assignment}}
56e2669397SChandler Carruth        x == 7; // No warning -- result is used
57e2669397SChandler Carruth        x == 7) // expected-warning {{equality comparison result unused}} \
58e2669397SChandler Carruth                // expected-note {{use '=' to turn this equality comparison into an assignment}}
59e2669397SChandler Carruth     x == 7; // expected-warning {{equality comparison result unused}} \
60e2669397SChandler Carruth             // expected-note {{use '=' to turn this equality comparison into an assignment}}
612b949c2bSChandler Carruth   switch (42) default: x == 7; // expected-warning {{equality comparison result unused}} \
622b949c2bSChandler Carruth                                // expected-note {{use '=' to turn this equality comparison into an assignment}}
632b949c2bSChandler Carruth   switch (42) case 42: x == 7; // expected-warning {{equality comparison result unused}} \
642b949c2bSChandler Carruth                                // expected-note {{use '=' to turn this equality comparison into an assignment}}
65e2669397SChandler Carruth   switch (42) {
66e2669397SChandler Carruth     case 1:
67e2669397SChandler Carruth     case 2:
68e2669397SChandler Carruth     default:
69e2669397SChandler Carruth     case 3:
70e2669397SChandler Carruth     case 4:
712b949c2bSChandler Carruth       x == 7; // expected-warning {{equality comparison result unused}} \
722b949c2bSChandler Carruth               // expected-note {{use '=' to turn this equality comparison into an assignment}}
73e2669397SChandler Carruth   }
74e2669397SChandler Carruth 
75e2669397SChandler Carruth   (void)(x == 7);
76e2669397SChandler Carruth   (void)(p == p); // expected-warning {{self-comparison always evaluates to true}}
77e2669397SChandler Carruth   { bool b = x == 7; }
78e2669397SChandler Carruth 
79e2669397SChandler Carruth   { bool b = ({ x == 7; // expected-warning {{equality comparison result unused}} \
80e2669397SChandler Carruth                         // expected-note {{use '=' to turn this equality comparison into an assignment}}
81e2669397SChandler Carruth                 x == 7; }); } // no warning on the second, its result is used!
82e2669397SChandler Carruth 
83e2669397SChandler Carruth #define EQ(x,y) (x) == (y)
84e2669397SChandler Carruth   EQ(x, 5);
85e2669397SChandler Carruth #undef EQ
86*78ecb873SAaron Ballman 
87*78ecb873SAaron Ballman   (void)sizeof(1 < 2, true); // No warning; unevaluated context.
88e2669397SChandler Carruth }
896e8da6a2SDouglas Gregor 
906e8da6a2SDouglas Gregor namespace PR10291 {
916e8da6a2SDouglas Gregor   template<typename T>
926e8da6a2SDouglas Gregor   class X
936e8da6a2SDouglas Gregor   {
946e8da6a2SDouglas Gregor   public:
956e8da6a2SDouglas Gregor 
X()966e8da6a2SDouglas Gregor     X() : i(0) { }
976e8da6a2SDouglas Gregor 
foo()986e8da6a2SDouglas Gregor     void foo()
996e8da6a2SDouglas Gregor     {
1006e8da6a2SDouglas Gregor       throw
1016e8da6a2SDouglas Gregor         i == 0u ?
1026e8da6a2SDouglas Gregor         5 : 6;
1036e8da6a2SDouglas Gregor     }
1046e8da6a2SDouglas Gregor 
1056e8da6a2SDouglas Gregor   private:
1066e8da6a2SDouglas Gregor     int i;
1076e8da6a2SDouglas Gregor   };
1086e8da6a2SDouglas Gregor 
1096e8da6a2SDouglas Gregor   X<int> x;
1106e8da6a2SDouglas Gregor }
111161132b9SRichard Trieu 
112161132b9SRichard Trieu namespace PR19724 {
113161132b9SRichard Trieu class stream {
114161132b9SRichard Trieu } cout, cin;
115161132b9SRichard Trieu 
116161132b9SRichard Trieu stream &operator<(stream &s, int);
117161132b9SRichard Trieu bool operator<(stream &s, stream &s2);
118161132b9SRichard Trieu 
test()119161132b9SRichard Trieu void test() {
120c3f36af8SAlp Toker   cout < 5;    // no warning, operator returns a reference
121161132b9SRichard Trieu   cout < cin;  // expected-warning {{relational comparison result unused}}
122161132b9SRichard Trieu }
123161132b9SRichard Trieu }
124ccedd527SRichard Trieu 
125ccedd527SRichard Trieu namespace PR19791 {
126ccedd527SRichard Trieu struct S {
127ccedd527SRichard Trieu   void operator!=(int);
128ccedd527SRichard Trieu   int operator==(int);
129ccedd527SRichard Trieu };
130ccedd527SRichard Trieu 
test()131ccedd527SRichard Trieu void test() {
132ccedd527SRichard Trieu   S s;
133ccedd527SRichard Trieu   s != 1;
134ccedd527SRichard Trieu   s == 1;  // expected-warning{{equality comparison result unused}}
135ccedd527SRichard Trieu            // expected-note@-1{{use '=' to turn this equality comparison into an assignment}}
136ccedd527SRichard Trieu }
137ccedd527SRichard Trieu }
138