aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-2.c
blob: 4441be422a19ca7e328525579396ab8c6d2bf9a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* Test C23 nodiscard attribute: invalid contexts.  */
/* { dg-do compile } */
/* { dg-options "-std=c23 -pedantic-errors" } */

/* This attribute is not valid on types other than their definitions,
   or on declarations other than function declarations, or on
   statements, or as an attribute-declaration.  */

[[nodiscard]]; /* { dg-error "ignored" } */

int [[nodiscard]] var; /* { dg-error "ignored" } */

int [[nodiscard ("reason")]] var2; /* { dg-error "ignored" } */

int array_with_nod_type[2] [[nodiscard]]; /* { dg-error "ignored" } */

void fn_with_nod_type () [[nodiscard]]; /* { dg-error "ignored" } */

int z = sizeof (int [[__nodiscard__]]); /* { dg-error "ignored" } */

[[nodiscard]] typedef int nod_int; /* { dg-error "can only be applied" } */

[[nodiscard]] int nvar; /* { dg-error "can only be applied" } */

struct s { int a; };

[[nodiscard]] typedef struct s nod_s; /* { dg-error "can only be applied" } */

struct t { [[nodiscard]] int b; }; /* { dg-error "can only be applied" } */

enum e { E [[nodiscard]] }; /* { dg-error "can only be applied" } */

void fx ([[nodiscard]] int p); /* { dg-error "can only be applied" } */

void
f (void)
{
  int a;
  [[nodiscard ("reason")]] int b = 1; /* { dg-error "can only be applied" } */
  [[nodiscard]]; /* { dg-error "ignored" } */
  [[nodiscard]] a = 1; /* { dg-error "ignored" } */
  [[nodiscard]] label: ; /* { dg-error "can only be applied" } */
  switch (var)
    {
      [[nodiscard]] case 1: ; /* { dg-error "can only be applied" } */
      [[nodiscard]] default: ; /* { dg-error "can only be applied" } */
    }
}