Argyrios Kyrtzidis | 1b2ad2f | 2010-09-19 23:03:35 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -verify %s |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 2 | template<typename T> void f() { |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 3 | T t; |
| 4 | t = 17; |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 5 | } |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 6 | |
Anders Carlsson | 6a8b7f3 | 2009-11-07 08:24:59 +0000 | [diff] [blame] | 7 | // PR5407 |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 8 | struct A { A(); }; |
| 9 | struct B { ~B(); }; |
Anders Carlsson | f7613d5 | 2009-11-07 07:26:56 +0000 | [diff] [blame] | 10 | void f() { |
| 11 | A a; |
| 12 | B b; |
Anders Carlsson | 6a8b7f3 | 2009-11-07 08:24:59 +0000 | [diff] [blame] | 13 | } |
Anders Carlsson | 58beed9 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 14 | |
| 15 | // PR5531 |
| 16 | namespace PR5531 { |
| 17 | struct A { |
| 18 | }; |
| 19 | |
| 20 | struct B { |
| 21 | B(int); |
| 22 | }; |
| 23 | |
| 24 | struct C { |
| 25 | ~C(); |
| 26 | }; |
| 27 | |
| 28 | void test() { |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 29 | A(); |
Anders Carlsson | 58beed9 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 30 | B(17); |
| 31 | C(); |
| 32 | } |
| 33 | } |
Nuno Lopes | cb1c77f | 2009-12-24 00:28:18 +0000 | [diff] [blame] | 34 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 35 | template<typename T> |
| 36 | struct X0 { }; |
| 37 | |
| 38 | template<typename T> |
| 39 | void test_dependent_init(T *p) { |
| 40 | X0<int> i(p); |
| 41 | (void)i; |
| 42 | } |
Douglas Gregor | a6a292b | 2010-04-27 16:20:13 +0000 | [diff] [blame] | 43 | |
Douglas Gregor | 268f89e | 2010-11-09 14:57:47 +0000 | [diff] [blame] | 44 | void unused_local_static() { |
| 45 | static int x = 0; |
| 46 | static int y = 0; // expected-warning{{unused variable 'y'}} |
| 47 | #pragma unused(x) |
| 48 | } |
Richard Smith | e3499ca | 2011-06-21 23:42:09 +0000 | [diff] [blame] | 49 | |
| 50 | // PR10168 |
| 51 | namespace PR10168 { |
| 52 | // We expect a warning in the definition only for non-dependent variables, and |
| 53 | // a warning in the instantiation only for dependent variables. |
| 54 | template<typename T> |
| 55 | struct S { |
| 56 | void f() { |
| 57 | int a; // expected-warning {{unused variable 'a'}} |
| 58 | T b; // expected-warning 2{{unused variable 'b'}} |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | template<typename T> |
| 63 | void f() { |
| 64 | int a; // expected-warning {{unused variable 'a'}} |
| 65 | T b; // expected-warning 2{{unused variable 'b'}} |
| 66 | } |
| 67 | |
| 68 | void g() { |
| 69 | S<int>().f(); // expected-note {{here}} |
| 70 | S<char>().f(); // expected-note {{here}} |
| 71 | f<int>(); // expected-note {{here}} |
| 72 | f<char>(); // expected-note {{here}} |
| 73 | } |
| 74 | } |
Rafael Espindola | 1a5d355 | 2012-01-06 04:54:01 +0000 | [diff] [blame] | 75 | |
| 76 | namespace PR11550 { |
| 77 | struct S1 { |
| 78 | S1(); |
| 79 | }; |
| 80 | S1 makeS1(); |
| 81 | void testS1(S1 a) { |
| 82 | // This constructor call can be elided. |
| 83 | S1 x = makeS1(); // expected-warning {{unused variable 'x'}} |
| 84 | |
| 85 | // This one cannot, so no warning. |
| 86 | S1 y; |
| 87 | |
| 88 | // This call cannot, but the constructor is trivial. |
| 89 | S1 z = a; // expected-warning {{unused variable 'z'}} |
| 90 | } |
| 91 | |
| 92 | // The same is true even when we know thet constructor has side effects. |
| 93 | void foo(); |
| 94 | struct S2 { |
| 95 | S2() { |
| 96 | foo(); |
| 97 | } |
| 98 | }; |
| 99 | S2 makeS2(); |
| 100 | void testS2(S2 a) { |
| 101 | S2 x = makeS2(); // expected-warning {{unused variable 'x'}} |
| 102 | S2 y; |
| 103 | S2 z = a; // expected-warning {{unused variable 'z'}} |
| 104 | } |
| 105 | |
| 106 | // Or when the constructor is not declared by the user. |
| 107 | struct S3 { |
| 108 | S1 m; |
| 109 | }; |
| 110 | S3 makeS3(); |
| 111 | void testS3(S3 a) { |
| 112 | S3 x = makeS3(); // expected-warning {{unused variable 'x'}} |
| 113 | S3 y; |
| 114 | S3 z = a; // expected-warning {{unused variable 'z'}} |
| 115 | } |
| 116 | } |
David Blaikie | 39e1776 | 2012-10-24 21:29:06 +0000 | [diff] [blame] | 117 | |
| 118 | namespace ctor_with_cleanups { |
| 119 | struct S1 { |
| 120 | ~S1(); |
| 121 | }; |
| 122 | struct S2 { |
| 123 | S2(const S1&); |
| 124 | }; |
| 125 | void func() { |
| 126 | S2 s((S1())); |
| 127 | } |
| 128 | } |
Matt Beaumont-Gay | 19b6a70 | 2013-04-10 00:47:10 +0000 | [diff] [blame] | 129 | |
| 130 | #include "Inputs/warn-unused-variables.h" |