Clang Project

clang_source_code/test/Sema/c2x-nodiscard.c
1// RUN: %clang_cc1 -fsyntax-only -fdouble-square-bracket-attributes -verify %s
2
3struct [[nodiscard]] S1 { // ok
4  int i;
5};
6struct [[nodiscard nodiscard]] S2 { // expected-error {{attribute 'nodiscard' cannot appear multiple times in an attribute specifier}}
7  int i;
8};
9struct [[nodiscard("Wrong")]] S3 { // expected-error {{'nodiscard' cannot have an argument list}}
10  int i;
11};
12
13[[nodiscard]] int f1(void);
14enum [[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
18struct [[nodiscard]] S4 {
19  int i;
20};
21struct S4 get_s(void);
22
23enum [[nodiscard]] E2 { Two };
24enum E2 get_e(void);
25
26[[nodiscard]] int get_i();
27
28void 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
39struct [[nodiscard]] error_info{
40  int i;
41};
42
43struct error_info enable_missile_safety_mode(void);
44void launch_missiles(void);
45void test_missiles(void) {
46  enable_missile_safety_mode(); // expected-warning {{ignoring return value of function declared with 'nodiscard'}}
47  launch_missiles();
48}
49
50