warn-unused-result.cpp revision 51ceb7bab599ea7d39d290ff5e88e4a1f0f5bc5c
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s 2 3int f() __attribute__((warn_unused_result)); 4 5struct S { 6 void t() const; 7}; 8S g1() __attribute__((warn_unused_result)); 9S *g2() __attribute__((warn_unused_result)); 10S &g3() __attribute__((warn_unused_result)); 11 12void test() { 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}} 17 18 (void)f(); 19 (void)g1(); 20 (void)g2(); 21 (void)g3(); 22 23 if (f() == 0) return; 24 25 g1().t(); 26 g2()->t(); 27 g3().t(); 28 29 int i = f(); 30 S s1 = g1(); 31 S *s2 = g2(); 32 S &s3 = g3(); 33 const S &s4 = g1(); 34} 35 36struct X { 37 int foo() __attribute__((warn_unused_result)); 38}; 39 40void bah() { 41 X x, *x2; 42 x.foo(); // expected-warning {{ignoring return value}} 43 x2->foo(); // expected-warning {{ignoring return value}} 44} 45 46namespace warn_unused_CXX11 { 47struct [[warn_unused_result]] Status { 48 bool ok() const; 49}; 50Status DoSomething(); 51Status& DoSomethingElse(); 52Status* DoAnotherThing(); 53Status** DoYetAnotherThing(); 54void lazy() { 55 Status s = DoSomething(); 56 if (!s.ok()) return; 57 Status &rs = DoSomethingElse(); 58 if (!rs.ok()) return; 59 Status *ps = DoAnotherThing(); 60 if (!ps->ok()) return; 61 Status **pps = DoYetAnotherThing(); 62 if (!(*pps)->ok()) return; 63 64 (void)DoSomething(); 65 (void)DoSomethingElse(); 66 (void)DoAnotherThing(); 67 (void)DoYetAnotherThing(); 68 69 DoSomething(); // expected-warning {{ignoring return value}} 70 DoSomethingElse(); // expected-warning {{ignoring return value}} 71 DoAnotherThing(); // expected-warning {{ignoring return value}} 72 DoYetAnotherThing(); 73} 74} 75