1 // RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value -std=c++11 %s
4 // RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value -std=c++17 %s
15 // pointer to volatile has side effect (thus no warning)
17 box
->i
; // expected-warning {{expression result unused}}
19 #if __cplusplus <= 199711L
20 // expected-warning@-2 {{expression result unused}}
28 bool operator==(const Foo
& rhs
) {
34 void b(Foo f1
, Foo f2
) {
35 NOP(f1
== f2
); // expected-warning {{expression result unused}}
43 template<typename T
> struct basic_string
{
47 &x
[0]; // expected-warning {{expression result unused}}
50 typedef basic_string
<char> string
;
51 void func(const std::string
& str
) {
52 str
.method(); // expected-note {{in instantiation of member function}}
65 struct __attribute__((warn_unused
)) Unused
{
75 Unused(); // expected-warning {{expression result unused}}
76 Unused(1); // expected-warning {{expression result unused}}
77 Unused(1, 1); // expected-warning {{expression result unused}}
78 #if __cplusplus >= 201103L // C++11 or later
80 Unused({}); // expected-warning {{expression result unused}}
90 struct Good
{ Good
&f(); };
91 struct Bad
{ virtual Bad
& f(); };
95 (void)typeid(++i
); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
98 (void)typeid(g
.f()); // Ok; not a polymorphic use of a glvalue.
100 // This is a polymorphic use of a glvalue, which results in the typeid being
101 // evaluated instead of unevaluated.
103 (void)typeid(b
.f()); // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
106 // This typeid can throw but that is not a side-effect that we care about
107 // warning for since this is idiomatic code
109 (void)sizeof(typeid(*pb
));
110 (void)typeid(*++pb
); // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
111 (void)sizeof(typeid(*++pb
)); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
112 // FIXME: we should not warn about this in an unevaluated context
113 // expected-warning@-2 {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
115 // A dereference of a volatile pointer is a side effecting operation, however
116 // since it is idiomatic code, and the alternatives induce higher maintenance
117 // costs, it is allowed.
119 (void)sizeof(*x
); // Ok
123 static volatile char var1
= 'a';
124 volatile char var2
= 'a';
125 static volatile char arr1
[] = "hello";
126 volatile char arr2
[] = "hello";
127 void volatile_array() {
128 static volatile char var3
= 'a';
129 volatile char var4
= 'a';
130 static volatile char arr3
[] = "hello";
131 volatile char arr4
[] = "hello";
133 // These all result in volatile loads in C and C++11. In C++98, they don't,
134 // but we suppress the warning in the case where '(void)var;' might be
135 // idiomatically suppressing an 'unused variable' warning.
138 #if __cplusplus < 201103L
139 // expected-warning@-2 {{expression result unused; assign into a variable to force a volatile load}}
144 // None of these result in volatile loads in any language mode, and it's not
145 // really reasonable to assume that they would, since volatile array loads
146 // don't really exist anywhere.
153 #if __cplusplus >= 201103L // C++11 or later
155 int v
[(5, 6)]; // expected-warning {{left operand of comma operator has no effect}}
157 new double[false ? (1, 2) : 3]
158 // FIXME: We shouldn't diagnose the unreachable constant expression
160 [false ? (1, 2) : 3]; // expected-warning {{left operand of comma operator has no effect}}
164 // comma operator diagnostics should be suppressed in a SFINAE context.
165 template <typename T
, int = (T
{},0)> int c(int) { return 0; }
166 template <typename T
, int> int c(double) { return 1; }
167 int foo() { return c
<int>(0); }
171 #if __cplusplus >= 201703L // C++17 or later
177 return (1.0,0.0); // expected-warning {{left operand of comma operator has no effect}}