xref: /llvm-project/clang/test/SemaCXX/unused.cpp (revision 6dd90616c477d83c156eed62c880e951bb508cfd)
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wunused %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 -Wunused %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wunused %s
4 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 -Wunused %s
5 
6 // PR4103 : Make sure we don't get a bogus unused expression warning
7 namespace PR4103 {
8   class APInt {
9     char foo; // expected-warning {{private field 'foo' is not used}}
10   };
11   class APSInt : public APInt {
12     char bar; // expected-warning {{private field 'bar' is not used}}
13   public:
14     APSInt &operator=(const APSInt &RHS);
15   };
16 
operator =(const APSInt & RHS)17   APSInt& APSInt::operator=(const APSInt &RHS) {
18     APInt::operator=(RHS);
19     return *this;
20   }
21 
22   template<typename T>
23   struct X {
24     X();
25   };
26 
test()27   void test() {
28     X<int>();
29   }
30 }
31 
32 namespace derefvolatile {
f(volatile char * x)33   void f(volatile char* x) {
34     *x;
35 #if __cplusplus <= 199711L
36     // expected-warning@-2 {{expression result unused; assign into a variable to force a volatile load}}
37 #endif
38     (void)*x;
39 #if __cplusplus <= 199711L
40     // expected-warning@-2 {{expression result unused; assign into a variable to force a volatile load}}
41 #endif
42     volatile char y = 10;
43     (void)y; // don't warn here, because it's a common pattern.
44   }
45 }
46 
47 namespace AnonObject {
48   struct Foo {
49     Foo(const char* const message);
50     ~Foo();
51   };
f()52   void f() {
53     Foo("Hello World!");  // don't warn
54     int(1); // expected-warning {{expression result unused}}
55   }
56 }
57 
58 // Test that constructing an object (which may have side effects) with
59 // constructor arguments which are dependent doesn't produce an unused value
60 // warning.
61 namespace UnresolvedLookup {
62   struct Foo {
63     Foo(int i, int j);
64   };
65   template <typename T>
66   struct Bar {
fUnresolvedLookup::Bar67     void f(T t) {
68       Foo(t, 0);  // no warning
69     }
70   };
71 }
72 
73 #if __cplusplus >= 201703L
74 namespace PR33839 {
a()75   void a() {
76     struct X { int a, b; } x;
77     auto [a, b] = x; // expected-warning {{unused variable '[a, b]'}}
78     auto [c, d] = x;
79     (void)d;
80   }
81 
f()82   template<typename T> void f() {
83     struct A { int n; } a[1];
84     for (auto [x] : a) {
85       (void)x;
86     }
87     auto [y] = a[0]; // expected-warning {{unused}}
88   }
g()89   template<bool b> void g() {
90     struct A { int n; } a[1];
91     for (auto [x] : a) {
92       if constexpr (b)
93         (void)x;
94     }
95 
96     auto [y] = a[0];
97     if constexpr (b)
98       (void)y; // ok, even when b == false
99   }
h()100   template<typename T> void h() {
101     struct A { int n; } a[1];
102     for (auto [x] : a) { // expected-warning {{unused variable '[x]'}}
103     }
104   }
use()105   void use() {
106     f<int>(); // expected-note {{instantiation of}}
107     g<true>();
108     g<false>();
109     h<int>(); // expected-note {{instantiation of}}
110   }
111 }
112 
113 namespace maybe_unused_binding {
114 
test()115 void test() {
116   struct X { int a, b; } x;
117   auto [a [[maybe_unused]], b] = x; // expected-warning {{an attribute specifier sequence attached to a structured binding declaration is a C++2c extension}}
118 }
119 
120 }
121 
122 #endif
123