1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
3 int f() __attribute__((warn_unused_result
));
8 S
g1() __attribute__((warn_unused_result
));
9 S
*g2() __attribute__((warn_unused_result
));
10 S
&g3() __attribute__((warn_unused_result
));
13 f(); // expected-warning {{ignoring return value}}
14 g1(); // expected-warning {{ignoring return value}}
15 g2(); // expected-warning {{ignoring return value}}
16 g3(); // expected-warning {{ignoring return value}}
36 void testSubstmts(int i
) {
39 f(); // expected-warning {{ignoring return value}}
41 f(); // expected-warning {{ignoring return value}}
45 f(); // expected-warning {{ignoring return value}}
47 f(); // expected-warning {{ignoring return value}}
50 f(); // expected-warning {{ignoring return value}}
53 f(); // expected-warning {{ignoring return value}}
56 for (f(); // expected-warning {{ignoring return value}}
58 f() // expected-warning {{ignoring return value}}
60 f(); // expected-warning {{ignoring return value}}
62 f(), // expected-warning {{ignoring return value}}
67 int foo() __attribute__((warn_unused_result
));
72 x
.foo(); // expected-warning {{ignoring return value}}
73 x2
->foo(); // expected-warning {{ignoring return value}}
76 namespace warn_unused_CXX11
{
83 struct [[clang::warn_unused_result
]] Status
{
85 Status
& operator=(const Status
& x
);
86 inline void Update(const Status
& new_status
) {
88 *this = new_status
; //no-warning
93 Status
& DoSomethingElse();
94 Status
* DoAnotherThing();
95 Status
** DoYetAnotherThing();
97 Status s
= DoSomething();
99 Status
&rs
= DoSomethingElse();
100 if (!rs
.ok()) return;
101 Status
*ps
= DoAnotherThing();
102 if (!ps
->ok()) return;
103 Status
**pps
= DoYetAnotherThing();
104 if (!(*pps
)->ok()) return;
107 (void)DoSomethingElse();
108 (void)DoAnotherThing();
109 (void)DoYetAnotherThing();
111 DoSomething(); // expected-warning {{ignoring return value}}
117 template <typename T
>
118 class [[clang::warn_unused_result
]] StatusOr
{
120 StatusOr
<int> doit();
123 f
.doStuff(); // expected-warning {{ignoring return value}}
124 doit(); // expected-warning {{ignoring return value}}
126 auto func
= []() { return Status(); };
127 func(); // expected-warning {{ignoring return value}}
132 struct [[clang::warn_unused_result
]] Status
;
142 f
.Bar(); // expected-warning {{ignoring return value}}
148 // Unevaluated contexts should not trigger unused result warnings.
149 template <typename T
>
150 auto foo(T
) -> decltype(f(), bool()) { // Should not warn.
164 // The typeid expression operand is evaluated only when the expression type is
165 // a glvalue of polymorphic class type.
178 // The typeid expression operand is evaluated only when the expression type is
179 // a glvalue of polymorphic class type; otherwise the expression operand is not
180 // evaluated and should not trigger a diagnostic.
183 (void)typeid(f(), c
); // Should not warn.
184 (void)typeid(f(), d
); // expected-warning {{ignoring return value}} expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
186 // The sizeof expression operand is never evaluated.
187 (void)sizeof(f(), c
); // Should not warn.
189 // The noexcept expression operand is never evaluated.
190 (void)noexcept(f(), false); // Should not warn.
195 // C++ Methods should warn even in their own class.
196 struct [[clang::warn_unused_result
]] S
{
197 S
DoThing() { return {}; };
198 S
operator++(int) { return {}; };
199 S
operator--(int) { return {}; };
200 // Improperly written prefix.
201 S
operator++() { return {}; };
202 S
operator--() { return {}; };
205 struct [[clang::warn_unused_result
]] P
{
206 P
DoThing() { return {}; };
209 P
operator++(const P
&, int) { return {}; };
210 P
operator--(const P
&, int) { return {}; };
211 // Improperly written prefix.
212 P
operator++(const P
&) { return {}; };
213 P
operator--(const P
&) { return {}; };
218 s
.DoThing(); // expected-warning {{ignoring return value}}
219 p
.DoThing(); // expected-warning {{ignoring return value}}
220 // Only postfix is expected to warn when written correctly.
221 s
++; // expected-warning {{ignoring return value}}
222 s
--; // expected-warning {{ignoring return value}}
223 p
++; // expected-warning {{ignoring return value}}
224 p
--; // expected-warning {{ignoring return value}}
225 // Improperly written prefix operators should still warn.
226 ++s
; // expected-warning {{ignoring return value}}
227 --s
; // expected-warning {{ignoring return value}}
228 ++p
; // expected-warning {{ignoring return value}}
229 --p
; // expected-warning {{ignoring return value}}
231 // Silencing the warning by cast to void still works.
241 [[clang::warn_unused_result
]] int f(int);
246 f(b
); // expected-warning {{ignoring return value}}
248 } // namespace PR39837
251 [[nodiscard
]] bool (*f
)(); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}}
252 [[clang::warn_unused_result
]] bool (*g
)();
253 __attribute__((warn_unused_result
)) bool (*h
)();
255 void i([[nodiscard
]] bool (*fp
)()); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}}