summaryrefslogtreecommitdiffstats
path: root/clang/test/SemaCXX/nodiscard.cpp
blob: 4eb004c67dfeedd5f406318bea74df6e91a69d96 (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
// RUN: %clang_cc1 -fsyntax-only -std=c++1z -verify -Wc++1z-extensions %s
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify -DEXT -Wc++1z-extensions %s

struct [[nodiscard]] S {};
S get_s();
S& get_s_ref();

enum [[nodiscard]] E {};
E get_e();

[[nodiscard]] int get_i();

void f() {
  get_s(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
  get_i(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
  get_e(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}

  // Okay, warnings are not encouraged
  get_s_ref();
  (void)get_s();
  (void)get_i();
  (void)get_e();
}

[[nodiscard nodiscard]] int wrong1(); // expected-error {{attribute 'nodiscard' cannot appear multiple times in an attribute specifier}}

namespace [[nodiscard]] N {} // expected-warning {{'nodiscard' attribute only applies to functions, methods, enums, and classes}}

#ifdef EXT
// expected-warning@4 {{use of the 'nodiscard' attribute is a C++1z extension}}
// expected-warning@8 {{use of the 'nodiscard' attribute is a C++1z extension}}
// expected-warning@11 {{use of the 'nodiscard' attribute is a C++1z extension}}
// expected-warning@25 2{{use of the 'nodiscard' attribute is a C++1z extension}}
#endif
OpenPOWER on IntegriCloud