summaryrefslogtreecommitdiff
path: root/clang/test/SemaCXX/warn-unused-comparison.cpp
blob: 0153f213ba1fccb50770a6603ad8371eb0f94b7f (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -Wno-unused -Wunused-comparison %s

struct A {
  bool operator==(const A&);
  bool operator!=(const A&);
  A operator|=(const A&);
  operator bool();
};

void test() {
  int x, *p;
  A a, b;

  x == 7; // expected-warning {{equality comparison result unused}} \
          // expected-note {{use '=' to turn this equality comparison into an assignment}}
  x != 7; // expected-warning {{inequality comparison result unused}} \
          // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
  7 == x; // expected-warning {{equality comparison result unused}}
  p == p; // expected-warning {{equality comparison result unused}} \
          // expected-note {{use '=' to turn this equality comparison into an assignment}} \
          // expected-warning {{self-comparison always evaluates to true}}
  a == a; // expected-warning {{equality comparison result unused}} \
          // expected-note {{use '=' to turn this equality comparison into an assignment}}
  a == b; // expected-warning {{equality comparison result unused}} \
          // expected-note {{use '=' to turn this equality comparison into an assignment}}
  a != b; // expected-warning {{inequality comparison result unused}} \
          // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
  A() == b; // expected-warning {{equality comparison result unused}}
  if (42) x == 7; // expected-warning {{equality comparison result unused}} \
                  // expected-note {{use '=' to turn this equality comparison into an assignment}}
  else if (42) x == 7; // expected-warning {{equality comparison result unused}} \
                       // expected-note {{use '=' to turn this equality comparison into an assignment}}
  else x == 7; // expected-warning {{equality comparison result unused}} \
               // expected-note {{use '=' to turn this equality comparison into an assignment}}
  do x == 7; // expected-warning {{equality comparison result unused}} \
             // expected-note {{use '=' to turn this equality comparison into an assignment}}
  while (false);
  while (false) x == 7; // expected-warning {{equality comparison result unused}} \
                        // expected-note {{use '=' to turn this equality comparison into an assignment}}
  for (x == 7; // expected-warning {{equality comparison result unused}} \
               // expected-note {{use '=' to turn this equality comparison into an assignment}}
       x == 7; // No warning -- result is used
       x == 7) // expected-warning {{equality comparison result unused}} \
               // expected-note {{use '=' to turn this equality comparison into an assignment}}
    x == 7; // expected-warning {{equality comparison result unused}} \
            // expected-note {{use '=' to turn this equality comparison into an assignment}}
  switch (42) default: x == 7; // expected-warning {{equality comparison result unused}} \
                               // expected-note {{use '=' to turn this equality comparison into an assignment}}
  switch (42) case 42: x == 7; // expected-warning {{equality comparison result unused}} \
                               // expected-note {{use '=' to turn this equality comparison into an assignment}}
  switch (42) {
    case 1:
    case 2:
    default:
    case 3:
    case 4:
      x == 7; // expected-warning {{equality comparison result unused}} \
              // expected-note {{use '=' to turn this equality comparison into an assignment}}
  }

  (void)(x == 7);
  (void)(p == p); // expected-warning {{self-comparison always evaluates to true}}
  { bool b = x == 7; }

  { bool b = ({ x == 7; // expected-warning {{equality comparison result unused}} \
                        // expected-note {{use '=' to turn this equality comparison into an assignment}}
                x == 7; }); } // no warning on the second, its result is used!

#define EQ(x,y) (x) == (y)
  EQ(x, 5);
#undef EQ
}

namespace PR10291 {
  template<typename T>
  class X
  {
  public:

    X() : i(0) { } 

    void foo()
    {   
      throw 
        i == 0u ?
        5 : 6;
    }   

  private:
    int i;
  };

  X<int> x;
}