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}}
37 int foo() __attribute__((warn_unused_result));
42 x.foo(); // expected-warning {{ignoring return value}}
43 x2->foo(); // expected-warning {{ignoring return value}}
46 namespace warn_unused_CXX11 {
53 struct [[clang::warn_unused_result]] Status {
55 Status& operator=(const Status& x);
56 inline void Update(const Status& new_status) {
58 *this = new_status; //no-warning
63 Status& DoSomethingElse();
64 Status* DoAnotherThing();
65 Status** DoYetAnotherThing();
67 Status s = DoSomething();
69 Status &rs = DoSomethingElse();
71 Status *ps = DoAnotherThing();
72 if (!ps->ok()) return;
73 Status **pps = DoYetAnotherThing();
74 if (!(*pps)->ok()) return;
77 (void)DoSomethingElse();
78 (void)DoAnotherThing();
79 (void)DoYetAnotherThing();
81 DoSomething(); // expected-warning {{ignoring return value}}
88 class [[clang::warn_unused_result]] StatusOr {
93 f.doStuff(); // expected-warning {{ignoring return value}}
94 doit(); // expected-warning {{ignoring return value}}
96 auto func = []() { return Status(); };
97 func(); // expected-warning {{ignoring return value}}
102 struct [[clang::warn_unused_result]] Status;
112 f.Bar(); // expected-warning {{ignoring return value}}
118 // Unevaluated contexts should not trigger unused result warnings.
119 template <typename T>
120 auto foo(T) -> decltype(f(), bool()) { // Should not warn.
134 // The typeid expression operand is evaluated only when the expression type is
135 // a glvalue of polymorphic class type.
148 // The typeid expression operand is evaluated only when the expression type is
149 // a glvalue of polymorphic class type; otherwise the expression operand is not
150 // evaluated and should not trigger a diagnostic.
153 (void)typeid(f(), c); // Should not warn.
154 (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'}}
156 // The sizeof expression operand is never evaluated.
157 (void)sizeof(f(), c); // Should not warn.
159 // The noexcept expression operand is never evaluated.
160 (void)noexcept(f(), false); // Should not warn.