warn-unused-comparison.cpp
5.39 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -Wno-unused -Wunused-comparison %s
struct A {
bool operator==(const A&);
bool operator!=(const A&);
bool operator<(const A&);
bool operator>(const 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}}
x < 7; // expected-warning {{relational comparison result unused}}
x > 7; // expected-warning {{relational comparison result unused}}
x <= 7; // expected-warning {{relational comparison result unused}}
x >= 7; // expected-warning {{relational comparison result unused}}
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 {{relational comparison result unused}}
a > b; // expected-warning {{relational comparison result unused}}
a <= b; // expected-warning {{relational comparison result unused}}
a >= b; // expected-warning {{relational comparison result unused}}
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
(void)sizeof(1 < 2, true); // No warning; unevaluated context.
}
namespace PR10291 {
template<typename T>
class X
{
public:
X() : i(0) { }
void foo()
{
throw
i == 0u ?
5 : 6;
}
private:
int i;
};
X<int> x;
}
namespace PR19724 {
class stream {
} cout, cin;
stream &operator<(stream &s, int);
bool operator<(stream &s, stream &s2);
void test() {
cout < 5; // no warning, operator returns a reference
cout < cin; // expected-warning {{relational comparison result unused}}
}
}
namespace PR19791 {
struct S {
void operator!=(int);
int operator==(int);
};
void test() {
S s;
s != 1;
s == 1; // expected-warning{{equality comparison result unused}}
// expected-note@-1{{use '=' to turn this equality comparison into an assignment}}
}
}