1 /* Test C23 nodiscard attribute: invalid contexts. */
2 /* { dg-do compile } */
3 /* { dg-options "-std=c23 -pedantic-errors" } */
5 /* This attribute is not valid on types other than their definitions,
6 or on declarations other than function declarations, or on
7 statements, or as an attribute-declaration. */
9 [[nodiscard
]]; /* { dg-error "ignored" } */
11 int [[nodiscard
]] var
; /* { dg-error "ignored" } */
13 int [[nodiscard ("reason")]] var2
; /* { dg-error "ignored" } */
15 int array_with_nod_type
[2] [[nodiscard
]]; /* { dg-error "ignored" } */
17 void fn_with_nod_type () [[nodiscard
]]; /* { dg-error "ignored" } */
19 int z
= sizeof (int [[__nodiscard__
]]); /* { dg-error "ignored" } */
21 [[nodiscard
]] typedef int nod_int
; /* { dg-error "can only be applied" } */
23 [[nodiscard
]] int nvar
; /* { dg-error "can only be applied" } */
27 [[nodiscard
]] typedef struct s nod_s
; /* { dg-error "can only be applied" } */
29 struct t
{ [[nodiscard
]] int b
; }; /* { dg-error "can only be applied" } */
31 enum e
{ E
[[nodiscard
]] }; /* { dg-error "can only be applied" } */
33 void fx ([[nodiscard
]] int p
); /* { dg-error "can only be applied" } */
39 [[nodiscard ("reason")]] int b
= 1; /* { dg-error "can only be applied" } */
40 [[nodiscard
]]; /* { dg-error "ignored" } */
41 [[nodiscard
]] a
= 1; /* { dg-error "ignored" } */
42 [[nodiscard
]] label
: ; /* { dg-error "can only be applied" } */
45 [[nodiscard
]] case 1: ; /* { dg-error "can only be applied" } */
46 [[nodiscard
]] default: ; /* { dg-error "can only be applied" } */