Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
Chandler Carruth | 34d4947 | 2011-02-21 00:56:56 +0000 | [diff] [blame] | 2 | |
| 3 | int f() __attribute__((warn_unused_result)); |
| 4 | |
| 5 | struct S { |
| 6 | void t() const; |
| 7 | }; |
| 8 | S g1() __attribute__((warn_unused_result)); |
| 9 | S *g2() __attribute__((warn_unused_result)); |
| 10 | S &g3() __attribute__((warn_unused_result)); |
| 11 | |
| 12 | void 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 | |
| 36 | struct X { |
| 37 | int foo() __attribute__((warn_unused_result)); |
| 38 | }; |
| 39 | |
| 40 | void bah() { |
| 41 | X x, *x2; |
| 42 | x.foo(); // expected-warning {{ignoring return value}} |
| 43 | x2->foo(); // expected-warning {{ignoring return value}} |
| 44 | } |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 45 | |
| 46 | namespace warn_unused_CXX11 { |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 47 | struct [[clang::warn_unused_result]] Status { |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 48 | bool ok() const; |
Kaelyn Uhrain | 97c81bf | 2012-11-13 21:23:31 +0000 | [diff] [blame] | 49 | Status& operator=(const Status& x); |
| 50 | inline void Update(const Status& new_status) { |
| 51 | if (ok()) { |
| 52 | *this = new_status; //no-warning |
| 53 | } |
| 54 | } |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 55 | }; |
| 56 | Status DoSomething(); |
| 57 | Status& DoSomethingElse(); |
| 58 | Status* DoAnotherThing(); |
| 59 | Status** DoYetAnotherThing(); |
| 60 | void lazy() { |
| 61 | Status s = DoSomething(); |
| 62 | if (!s.ok()) return; |
| 63 | Status &rs = DoSomethingElse(); |
| 64 | if (!rs.ok()) return; |
| 65 | Status *ps = DoAnotherThing(); |
| 66 | if (!ps->ok()) return; |
| 67 | Status **pps = DoYetAnotherThing(); |
| 68 | if (!(*pps)->ok()) return; |
| 69 | |
| 70 | (void)DoSomething(); |
| 71 | (void)DoSomethingElse(); |
| 72 | (void)DoAnotherThing(); |
| 73 | (void)DoYetAnotherThing(); |
| 74 | |
| 75 | DoSomething(); // expected-warning {{ignoring return value}} |
| 76 | DoSomethingElse(); // expected-warning {{ignoring return value}} |
| 77 | DoAnotherThing(); // expected-warning {{ignoring return value}} |
| 78 | DoYetAnotherThing(); |
| 79 | } |
| 80 | } |