(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
c2x-attr-nodiscard-2.c
       1  /* Test C2x nodiscard attribute: invalid contexts.  */
       2  /* { dg-do compile } */
       3  /* { dg-options "-std=c2x -pedantic-errors" } */
       4  
       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.  */
       8  
       9  [[nodiscard]]; /* { dg-error "ignored" } */
      10  
      11  int [[nodiscard]] var; /* { dg-error "ignored" } */
      12  
      13  int [[nodiscard ("reason")]] var2; /* { dg-error "ignored" } */
      14  
      15  int array_with_nod_type[2] [[nodiscard]]; /* { dg-error "ignored" } */
      16  
      17  void fn_with_nod_type () [[nodiscard]]; /* { dg-error "ignored" } */
      18  
      19  int z = sizeof (int [[__nodiscard__]]); /* { dg-error "ignored" } */
      20  
      21  [[nodiscard]] typedef int nod_int; /* { dg-error "can only be applied" } */
      22  
      23  [[nodiscard]] int nvar; /* { dg-error "can only be applied" } */
      24  
      25  struct s { int a; };
      26  
      27  [[nodiscard]] typedef struct s nod_s; /* { dg-error "can only be applied" } */
      28  
      29  struct t { [[nodiscard]] int b; }; /* { dg-error "can only be applied" } */
      30  
      31  enum e { E [[nodiscard]] }; /* { dg-error "can only be applied" } */
      32  
      33  void fx ([[nodiscard]] int p); /* { dg-error "can only be applied" } */
      34  
      35  void
      36  f (void)
      37  {
      38    int a;
      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" } */
      43    switch (var)
      44      {
      45        [[nodiscard]] case 1: ; /* { dg-error "can only be applied" } */
      46        [[nodiscard]] default: ; /* { dg-error "can only be applied" } */
      47      }
      48  }