| 1 |
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify=expected,cxx11,cxx11-17 -pedantic %s |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify=expected,cxx11-17,since-cxx17 -pedantic %s |
| 3 |
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify=expected,since-cxx17 -pedantic %s |
| 4 |
|
| 5 |
struct [[nodiscard]] S {}; |
| 6 |
// cxx11-warning@-1 {{use of the 'nodiscard' attribute is a C++17 extension}} |
| 7 |
S get_s(); |
| 8 |
S& get_s_ref(); |
| 9 |
|
| 10 |
enum [[nodiscard]] E {}; |
| 11 |
// cxx11-warning@-1 {{use of the 'nodiscard' attribute is a C++17 extension}} |
| 12 |
E get_e(); |
| 13 |
|
| 14 |
[[nodiscard]] int get_i(); |
| 15 |
// cxx11-warning@-1 {{use of the 'nodiscard' attribute is a C++17 extension}} |
| 16 |
[[nodiscard]] volatile int &get_vi(); |
| 17 |
// cxx11-warning@-1 {{use of the 'nodiscard' attribute is a C++17 extension}} |
| 18 |
|
| 19 |
void f() { |
| 20 |
get_s(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 21 |
get_i(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 22 |
get_vi(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 23 |
get_e(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 24 |
|
| 25 |
// Okay, warnings are not encouraged |
| 26 |
get_s_ref(); |
| 27 |
(void)get_s(); |
| 28 |
(void)get_i(); |
| 29 |
(void)get_vi(); |
| 30 |
(void)get_e(); |
| 31 |
} |
| 32 |
|
| 33 |
[[nodiscard]] volatile char &(*fp)(); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}} |
| 34 |
// cxx11-warning@-1 {{use of the 'nodiscard' attribute is a C++17 extension}} |
| 35 |
void g() { |
| 36 |
fp(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 37 |
|
| 38 |
// OK, warning suppressed. |
| 39 |
(void)fp(); |
| 40 |
} |
| 41 |
|
| 42 |
namespace PR31526 { |
| 43 |
typedef E (*fp1)(); |
| 44 |
typedef S (*fp2)(); |
| 45 |
|
| 46 |
typedef S S_alias; |
| 47 |
typedef S_alias (*fp3)(); |
| 48 |
|
| 49 |
typedef fp2 fp2_alias; |
| 50 |
|
| 51 |
void f() { |
| 52 |
fp1 one; |
| 53 |
fp2 two; |
| 54 |
fp3 three; |
| 55 |
fp2_alias four; |
| 56 |
|
| 57 |
one(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 58 |
two(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 59 |
three(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 60 |
four(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 61 |
|
| 62 |
// These are all okay because of the explicit cast to void. |
| 63 |
(void)one(); |
| 64 |
(void)two(); |
| 65 |
(void)three(); |
| 66 |
(void)four(); |
| 67 |
} |
| 68 |
} // namespace PR31526 |
| 69 |
|
| 70 |
struct [[nodiscard("reason")]] ReasonStruct {}; |
| 71 |
// cxx11-17-warning@-1 {{use of the 'nodiscard' attribute is a C++20 extension}} |
| 72 |
struct LaterReason; |
| 73 |
struct [[nodiscard("later reason")]] LaterReason {}; |
| 74 |
// cxx11-17-warning@-1 {{use of the 'nodiscard' attribute is a C++20 extension}} |
| 75 |
|
| 76 |
ReasonStruct get_reason(); |
| 77 |
LaterReason get_later_reason(); |
| 78 |
[[nodiscard("another reason")]] int another_reason(); |
| 79 |
// cxx11-17-warning@-1 {{use of the 'nodiscard' attribute is a C++20 extension}} |
| 80 |
|
| 81 |
[[nodiscard("conflicting reason")]] int conflicting_reason(); |
| 82 |
// cxx11-17-warning@-1 {{use of the 'nodiscard' attribute is a C++20 extension}} |
| 83 |
[[nodiscard("special reason")]] int conflicting_reason(); |
| 84 |
// cxx11-17-warning@-1 {{use of the 'nodiscard' attribute is a C++20 extension}} |
| 85 |
|
| 86 |
void cxx20_use() { |
| 87 |
get_reason(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute: reason}} |
| 88 |
get_later_reason(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute: later reason}} |
| 89 |
another_reason(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute: another reason}} |
| 90 |
conflicting_reason(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute: special reason}} |
| 91 |
} |
| 92 |
|
| 93 |
namespace p1771 { |
| 94 |
struct[[nodiscard("Don't throw me away!")]] ConvertTo{}; |
| 95 |
// cxx11-17-warning@-1 {{use of the 'nodiscard' attribute is a C++20 extension}} |
| 96 |
struct S { |
| 97 |
[[nodiscard]] S(); |
| 98 |
// cxx11-warning@-1 {{use of the 'nodiscard' attribute is a C++17 extension}} |
| 99 |
[[nodiscard("Don't let that S-Char go!")]] S(char); |
| 100 |
// cxx11-17-warning@-1 {{use of the 'nodiscard' attribute is a C++20 extension}} |
| 101 |
S(int); |
| 102 |
[[gnu::warn_unused_result]] S(double); |
| 103 |
operator ConvertTo(); |
| 104 |
[[nodiscard]] operator int(); |
| 105 |
// cxx11-warning@-1 {{use of the 'nodiscard' attribute is a C++17 extension}} |
| 106 |
[[nodiscard("Don't throw away as a double")]] operator double(); |
| 107 |
// cxx11-17-warning@-1 {{use of the 'nodiscard' attribute is a C++20 extension}} |
| 108 |
}; |
| 109 |
|
| 110 |
struct[[nodiscard("Don't throw me away either!")]] Y{}; |
| 111 |
// cxx11-17-warning@-1 {{use of the 'nodiscard' attribute is a C++20 extension}} |
| 112 |
|
| 113 |
void usage() { |
| 114 |
S(); // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard' attribute}} |
| 115 |
S('A'); // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't let that S-Char go!}} |
| 116 |
S(1); |
| 117 |
S(2.2); |
| 118 |
Y(); // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't throw me away either!}} |
| 119 |
S s; |
| 120 |
ConvertTo{}; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute: Don't throw me away!}} |
| 121 |
|
| 122 |
// AST is different in C++17 mode. Before, a move ctor for ConvertTo is there |
| 123 |
// as well, hence the constructor warning. |
| 124 |
|
| 125 |
// since-cxx17-warning@+2 {{ignoring return value of function declared with 'nodiscard' attribute: Don't throw me away!}} |
| 126 |
// cxx11-warning@+1 {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't throw me away!}} |
| 127 |
(ConvertTo) s; |
| 128 |
(int)s; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 129 |
(S)'c'; // expected-warning {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't let that S-Char go!}} |
| 130 |
// since-cxx17-warning@+2 {{ignoring return value of function declared with 'nodiscard' attribute: Don't throw me away!}} |
| 131 |
// cxx11-warning@+1 {{ignoring temporary created by a constructor declared with 'nodiscard' attribute: Don't throw me away!}} |
| 132 |
static_cast<ConvertTo>(s); |
| 133 |
static_cast<int>(s); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 134 |
static_cast<double>(s); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute: Don't throw away as a double}} |
| 135 |
} |
| 136 |
} // namespace p1771 |