xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-unused-comparison.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -Wno-unused -Wunused-comparison %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc struct A {
4f4a2713aSLionel Sambuc   bool operator==(const A&);
5f4a2713aSLionel Sambuc   bool operator!=(const A&);
6*0a6a1f1dSLionel Sambuc   bool operator<(const A&);
7*0a6a1f1dSLionel Sambuc   bool operator>(const A&);
8*0a6a1f1dSLionel Sambuc   bool operator<=(const A&);
9*0a6a1f1dSLionel Sambuc   bool operator>=(const A&);
10f4a2713aSLionel Sambuc   A operator|=(const A&);
11f4a2713aSLionel Sambuc   operator bool();
12f4a2713aSLionel Sambuc };
13f4a2713aSLionel Sambuc 
test()14f4a2713aSLionel Sambuc void test() {
15f4a2713aSLionel Sambuc   int x, *p;
16f4a2713aSLionel Sambuc   A a, b;
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc   x == 7; // expected-warning {{equality comparison result unused}} \
19f4a2713aSLionel Sambuc           // expected-note {{use '=' to turn this equality comparison into an assignment}}
20f4a2713aSLionel Sambuc   x != 7; // expected-warning {{inequality comparison result unused}} \
21f4a2713aSLionel Sambuc           // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
22*0a6a1f1dSLionel Sambuc   x < 7;  // expected-warning {{relational comparison result unused}}
23*0a6a1f1dSLionel Sambuc   x > 7;  // expected-warning {{relational comparison result unused}}
24*0a6a1f1dSLionel Sambuc   x <= 7; // expected-warning {{relational comparison result unused}}
25*0a6a1f1dSLionel Sambuc   x >= 7; // expected-warning {{relational comparison result unused}}
26*0a6a1f1dSLionel Sambuc 
27f4a2713aSLionel Sambuc   7 == x; // expected-warning {{equality comparison result unused}}
28f4a2713aSLionel Sambuc   p == p; // expected-warning {{equality comparison result unused}} \
29f4a2713aSLionel Sambuc           // expected-note {{use '=' to turn this equality comparison into an assignment}} \
30f4a2713aSLionel Sambuc           // expected-warning {{self-comparison always evaluates to true}}
31f4a2713aSLionel Sambuc   a == a; // expected-warning {{equality comparison result unused}} \
32f4a2713aSLionel Sambuc           // expected-note {{use '=' to turn this equality comparison into an assignment}}
33f4a2713aSLionel Sambuc   a == b; // expected-warning {{equality comparison result unused}} \
34f4a2713aSLionel Sambuc           // expected-note {{use '=' to turn this equality comparison into an assignment}}
35f4a2713aSLionel Sambuc   a != b; // expected-warning {{inequality comparison result unused}} \
36f4a2713aSLionel Sambuc           // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
37*0a6a1f1dSLionel Sambuc   a < b;  // expected-warning {{relational comparison result unused}}
38*0a6a1f1dSLionel Sambuc   a > b;  // expected-warning {{relational comparison result unused}}
39*0a6a1f1dSLionel Sambuc   a <= b; // expected-warning {{relational comparison result unused}}
40*0a6a1f1dSLionel Sambuc   a >= b; // expected-warning {{relational comparison result unused}}
41*0a6a1f1dSLionel Sambuc 
42f4a2713aSLionel Sambuc   A() == b; // expected-warning {{equality comparison result unused}}
43f4a2713aSLionel Sambuc   if (42) x == 7; // expected-warning {{equality comparison result unused}} \
44f4a2713aSLionel Sambuc                   // expected-note {{use '=' to turn this equality comparison into an assignment}}
45f4a2713aSLionel Sambuc   else if (42) x == 7; // expected-warning {{equality comparison result unused}} \
46f4a2713aSLionel Sambuc                        // expected-note {{use '=' to turn this equality comparison into an assignment}}
47f4a2713aSLionel Sambuc   else x == 7; // expected-warning {{equality comparison result unused}} \
48f4a2713aSLionel Sambuc                // expected-note {{use '=' to turn this equality comparison into an assignment}}
49f4a2713aSLionel Sambuc   do x == 7; // expected-warning {{equality comparison result unused}} \
50f4a2713aSLionel Sambuc              // expected-note {{use '=' to turn this equality comparison into an assignment}}
51f4a2713aSLionel Sambuc   while (false);
52f4a2713aSLionel Sambuc   while (false) x == 7; // expected-warning {{equality comparison result unused}} \
53f4a2713aSLionel Sambuc                         // expected-note {{use '=' to turn this equality comparison into an assignment}}
54f4a2713aSLionel Sambuc   for (x == 7; // expected-warning {{equality comparison result unused}} \
55f4a2713aSLionel Sambuc                // expected-note {{use '=' to turn this equality comparison into an assignment}}
56f4a2713aSLionel Sambuc        x == 7; // No warning -- result is used
57f4a2713aSLionel Sambuc        x == 7) // expected-warning {{equality comparison result unused}} \
58f4a2713aSLionel Sambuc                // expected-note {{use '=' to turn this equality comparison into an assignment}}
59f4a2713aSLionel Sambuc     x == 7; // expected-warning {{equality comparison result unused}} \
60f4a2713aSLionel Sambuc             // expected-note {{use '=' to turn this equality comparison into an assignment}}
61f4a2713aSLionel Sambuc   switch (42) default: x == 7; // expected-warning {{equality comparison result unused}} \
62f4a2713aSLionel Sambuc                                // expected-note {{use '=' to turn this equality comparison into an assignment}}
63f4a2713aSLionel Sambuc   switch (42) case 42: x == 7; // expected-warning {{equality comparison result unused}} \
64f4a2713aSLionel Sambuc                                // expected-note {{use '=' to turn this equality comparison into an assignment}}
65f4a2713aSLionel Sambuc   switch (42) {
66f4a2713aSLionel Sambuc     case 1:
67f4a2713aSLionel Sambuc     case 2:
68f4a2713aSLionel Sambuc     default:
69f4a2713aSLionel Sambuc     case 3:
70f4a2713aSLionel Sambuc     case 4:
71f4a2713aSLionel Sambuc       x == 7; // expected-warning {{equality comparison result unused}} \
72f4a2713aSLionel Sambuc               // expected-note {{use '=' to turn this equality comparison into an assignment}}
73f4a2713aSLionel Sambuc   }
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc   (void)(x == 7);
76f4a2713aSLionel Sambuc   (void)(p == p); // expected-warning {{self-comparison always evaluates to true}}
77f4a2713aSLionel Sambuc   { bool b = x == 7; }
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc   { bool b = ({ x == 7; // expected-warning {{equality comparison result unused}} \
80f4a2713aSLionel Sambuc                         // expected-note {{use '=' to turn this equality comparison into an assignment}}
81f4a2713aSLionel Sambuc                 x == 7; }); } // no warning on the second, its result is used!
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc #define EQ(x,y) (x) == (y)
84f4a2713aSLionel Sambuc   EQ(x, 5);
85f4a2713aSLionel Sambuc #undef EQ
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc   (void)sizeof(1 < 2, true); // No warning; unevaluated context.
88f4a2713aSLionel Sambuc }
89f4a2713aSLionel Sambuc 
90f4a2713aSLionel Sambuc namespace PR10291 {
91f4a2713aSLionel Sambuc   template<typename T>
92f4a2713aSLionel Sambuc   class X
93f4a2713aSLionel Sambuc   {
94f4a2713aSLionel Sambuc   public:
95f4a2713aSLionel Sambuc 
X()96f4a2713aSLionel Sambuc     X() : i(0) { }
97f4a2713aSLionel Sambuc 
foo()98f4a2713aSLionel Sambuc     void foo()
99f4a2713aSLionel Sambuc     {
100f4a2713aSLionel Sambuc       throw
101f4a2713aSLionel Sambuc         i == 0u ?
102f4a2713aSLionel Sambuc         5 : 6;
103f4a2713aSLionel Sambuc     }
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc   private:
106f4a2713aSLionel Sambuc     int i;
107f4a2713aSLionel Sambuc   };
108f4a2713aSLionel Sambuc 
109f4a2713aSLionel Sambuc   X<int> x;
110f4a2713aSLionel Sambuc }
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc namespace PR19724 {
113*0a6a1f1dSLionel Sambuc class stream {
114*0a6a1f1dSLionel Sambuc } cout, cin;
115*0a6a1f1dSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc stream &operator<(stream &s, int);
117*0a6a1f1dSLionel Sambuc bool operator<(stream &s, stream &s2);
118*0a6a1f1dSLionel Sambuc 
test()119*0a6a1f1dSLionel Sambuc void test() {
120*0a6a1f1dSLionel Sambuc   cout < 5;    // no warning, operator returns a reference
121*0a6a1f1dSLionel Sambuc   cout < cin;  // expected-warning {{relational comparison result unused}}
122*0a6a1f1dSLionel Sambuc }
123*0a6a1f1dSLionel Sambuc }
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc namespace PR19791 {
126*0a6a1f1dSLionel Sambuc struct S {
127*0a6a1f1dSLionel Sambuc   void operator!=(int);
128*0a6a1f1dSLionel Sambuc   int operator==(int);
129*0a6a1f1dSLionel Sambuc };
130*0a6a1f1dSLionel Sambuc 
test()131*0a6a1f1dSLionel Sambuc void test() {
132*0a6a1f1dSLionel Sambuc   S s;
133*0a6a1f1dSLionel Sambuc   s != 1;
134*0a6a1f1dSLionel Sambuc   s == 1;  // expected-warning{{equality comparison result unused}}
135*0a6a1f1dSLionel Sambuc            // expected-note@-1{{use '=' to turn this equality comparison into an assignment}}
136*0a6a1f1dSLionel Sambuc }
137*0a6a1f1dSLionel Sambuc }
138