xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-unused-comparison.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -Wno-unused -Wunused-comparison %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc struct A {
4*f4a2713aSLionel Sambuc   bool operator==(const A&);
5*f4a2713aSLionel Sambuc   bool operator!=(const A&);
6*f4a2713aSLionel Sambuc   A operator|=(const A&);
7*f4a2713aSLionel Sambuc   operator bool();
8*f4a2713aSLionel Sambuc };
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc void test() {
11*f4a2713aSLionel Sambuc   int x, *p;
12*f4a2713aSLionel Sambuc   A a, b;
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc   x == 7; // expected-warning {{equality comparison result unused}} \
15*f4a2713aSLionel Sambuc           // expected-note {{use '=' to turn this equality comparison into an assignment}}
16*f4a2713aSLionel Sambuc   x != 7; // expected-warning {{inequality comparison result unused}} \
17*f4a2713aSLionel Sambuc           // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
18*f4a2713aSLionel Sambuc   7 == x; // expected-warning {{equality comparison result unused}}
19*f4a2713aSLionel Sambuc   p == p; // expected-warning {{equality comparison result unused}} \
20*f4a2713aSLionel Sambuc           // expected-note {{use '=' to turn this equality comparison into an assignment}} \
21*f4a2713aSLionel Sambuc           // expected-warning {{self-comparison always evaluates to true}}
22*f4a2713aSLionel Sambuc   a == a; // expected-warning {{equality comparison result unused}} \
23*f4a2713aSLionel Sambuc           // expected-note {{use '=' to turn this equality comparison into an assignment}}
24*f4a2713aSLionel Sambuc   a == b; // expected-warning {{equality comparison result unused}} \
25*f4a2713aSLionel Sambuc           // expected-note {{use '=' to turn this equality comparison into an assignment}}
26*f4a2713aSLionel Sambuc   a != b; // expected-warning {{inequality comparison result unused}} \
27*f4a2713aSLionel Sambuc           // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
28*f4a2713aSLionel Sambuc   A() == b; // expected-warning {{equality comparison result unused}}
29*f4a2713aSLionel Sambuc   if (42) x == 7; // expected-warning {{equality comparison result unused}} \
30*f4a2713aSLionel Sambuc                   // expected-note {{use '=' to turn this equality comparison into an assignment}}
31*f4a2713aSLionel Sambuc   else if (42) x == 7; // expected-warning {{equality comparison result unused}} \
32*f4a2713aSLionel Sambuc                        // expected-note {{use '=' to turn this equality comparison into an assignment}}
33*f4a2713aSLionel Sambuc   else x == 7; // expected-warning {{equality comparison result unused}} \
34*f4a2713aSLionel Sambuc                // expected-note {{use '=' to turn this equality comparison into an assignment}}
35*f4a2713aSLionel Sambuc   do x == 7; // expected-warning {{equality comparison result unused}} \
36*f4a2713aSLionel Sambuc              // expected-note {{use '=' to turn this equality comparison into an assignment}}
37*f4a2713aSLionel Sambuc   while (false);
38*f4a2713aSLionel Sambuc   while (false) x == 7; // expected-warning {{equality comparison result unused}} \
39*f4a2713aSLionel Sambuc                         // expected-note {{use '=' to turn this equality comparison into an assignment}}
40*f4a2713aSLionel Sambuc   for (x == 7; // expected-warning {{equality comparison result unused}} \
41*f4a2713aSLionel Sambuc                // expected-note {{use '=' to turn this equality comparison into an assignment}}
42*f4a2713aSLionel Sambuc        x == 7; // No warning -- result is used
43*f4a2713aSLionel Sambuc        x == 7) // expected-warning {{equality comparison result unused}} \
44*f4a2713aSLionel Sambuc                // expected-note {{use '=' to turn this equality comparison into an assignment}}
45*f4a2713aSLionel Sambuc     x == 7; // expected-warning {{equality comparison result unused}} \
46*f4a2713aSLionel Sambuc             // expected-note {{use '=' to turn this equality comparison into an assignment}}
47*f4a2713aSLionel Sambuc   switch (42) default: x == 7; // expected-warning {{equality comparison result unused}} \
48*f4a2713aSLionel Sambuc                                // expected-note {{use '=' to turn this equality comparison into an assignment}}
49*f4a2713aSLionel Sambuc   switch (42) case 42: x == 7; // expected-warning {{equality comparison result unused}} \
50*f4a2713aSLionel Sambuc                                // expected-note {{use '=' to turn this equality comparison into an assignment}}
51*f4a2713aSLionel Sambuc   switch (42) {
52*f4a2713aSLionel Sambuc     case 1:
53*f4a2713aSLionel Sambuc     case 2:
54*f4a2713aSLionel Sambuc     default:
55*f4a2713aSLionel Sambuc     case 3:
56*f4a2713aSLionel Sambuc     case 4:
57*f4a2713aSLionel Sambuc       x == 7; // expected-warning {{equality comparison result unused}} \
58*f4a2713aSLionel Sambuc               // expected-note {{use '=' to turn this equality comparison into an assignment}}
59*f4a2713aSLionel Sambuc   }
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc   (void)(x == 7);
62*f4a2713aSLionel Sambuc   (void)(p == p); // expected-warning {{self-comparison always evaluates to true}}
63*f4a2713aSLionel Sambuc   { bool b = x == 7; }
64*f4a2713aSLionel Sambuc 
65*f4a2713aSLionel Sambuc   { bool b = ({ x == 7; // expected-warning {{equality comparison result unused}} \
66*f4a2713aSLionel Sambuc                         // expected-note {{use '=' to turn this equality comparison into an assignment}}
67*f4a2713aSLionel Sambuc                 x == 7; }); } // no warning on the second, its result is used!
68*f4a2713aSLionel Sambuc 
69*f4a2713aSLionel Sambuc #define EQ(x,y) (x) == (y)
70*f4a2713aSLionel Sambuc   EQ(x, 5);
71*f4a2713aSLionel Sambuc #undef EQ
72*f4a2713aSLionel Sambuc }
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc namespace PR10291 {
75*f4a2713aSLionel Sambuc   template<typename T>
76*f4a2713aSLionel Sambuc   class X
77*f4a2713aSLionel Sambuc   {
78*f4a2713aSLionel Sambuc   public:
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc     X() : i(0) { }
81*f4a2713aSLionel Sambuc 
82*f4a2713aSLionel Sambuc     void foo()
83*f4a2713aSLionel Sambuc     {
84*f4a2713aSLionel Sambuc       throw
85*f4a2713aSLionel Sambuc         i == 0u ?
86*f4a2713aSLionel Sambuc         5 : 6;
87*f4a2713aSLionel Sambuc     }
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc   private:
90*f4a2713aSLionel Sambuc     int i;
91*f4a2713aSLionel Sambuc   };
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc   X<int> x;
94*f4a2713aSLionel Sambuc }
95