| 1 | // RUN: %clang_cc1 -fsyntax-only -fdouble-square-bracket-attributes -verify %s |
| 2 | |
| 3 | struct [[nodiscard]] S1 { // ok |
| 4 | int i; |
| 5 | }; |
| 6 | struct [[nodiscard nodiscard]] S2 { // expected-error {{attribute 'nodiscard' cannot appear multiple times in an attribute specifier}} |
| 7 | int i; |
| 8 | }; |
| 9 | struct [[nodiscard("Wrong")]] S3 { // expected-error {{'nodiscard' cannot have an argument list}} |
| 10 | int i; |
| 11 | }; |
| 12 | |
| 13 | [[nodiscard]] int f1(void); |
| 14 | enum [[nodiscard]] E1 { One }; |
| 15 | |
| 16 | [[nodiscard]] int i; // expected-warning {{'nodiscard' attribute only applies to Objective-C methods, enums, structs, unions, classes, functions, and function pointers}} |
| 17 | |
| 18 | struct [[nodiscard]] S4 { |
| 19 | int i; |
| 20 | }; |
| 21 | struct S4 get_s(void); |
| 22 | |
| 23 | enum [[nodiscard]] E2 { Two }; |
| 24 | enum E2 get_e(void); |
| 25 | |
| 26 | [[nodiscard]] int get_i(); |
| 27 | |
| 28 | void f2(void) { |
| 29 | get_s(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 30 | get_i(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 31 | get_e(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| 32 | |
| 33 | // Okay, warnings are not encouraged |
| 34 | (void)get_s(); |
| 35 | (void)get_i(); |
| 36 | (void)get_e(); |
| 37 | } |
| 38 | |
| 39 | struct [[nodiscard]] error_info{ |
| 40 | int i; |
| 41 | }; |
| 42 | |
| 43 | struct error_info enable_missile_safety_mode(void); |
| 44 | void launch_missiles(void); |
| 45 | void test_missiles(void) { |
| 46 | enable_missile_safety_mode(); // expected-warning {{ignoring return value of function declared with 'nodiscard'}} |
| 47 | launch_missiles(); |
| 48 | } |
| 49 | |
| 50 | |