xref: /llvm-project/clang/test/SemaCXX/warn-uninitialized-const-reference.cpp (revision 054704082b461418d3dac3a379792cdaf52d40b3)
1*05470408SZequan Wu // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wuninitialized-const-reference -verify %s
2170b6869SZequan Wu 
3170b6869SZequan Wu class A {
4170b6869SZequan Wu public:
5170b6869SZequan Wu   int i;
A()6170b6869SZequan Wu   A(){};
A(const A & a)7170b6869SZequan Wu   A(const A &a){};
A(int i)8170b6869SZequan Wu   A(int i) {}
9170b6869SZequan Wu   bool operator!=(const A &);
10170b6869SZequan Wu };
11170b6869SZequan Wu 
12*05470408SZequan Wu template <class T>
ignore_template(const T &)13*05470408SZequan Wu void ignore_template(const T &) {}
ignore(const int & i)14*05470408SZequan Wu void ignore(const int &i) {}
dont_ignore_non_empty(const int & i)15*05470408SZequan Wu void dont_ignore_non_empty(const int &i) { ; } // Calling this won't silence the warning for you
dont_ignore_block(const int & i)16*05470408SZequan Wu void dont_ignore_block(const int &i) {
17*05470408SZequan Wu   {}
18*05470408SZequan Wu } // Calling this won't silence the warning for you
ignore_function_try_block_maybe_who_knows(const int &)19*05470408SZequan Wu void ignore_function_try_block_maybe_who_knows(const int &) try {
20*05470408SZequan Wu } catch (...) {
21*05470408SZequan Wu }
22170b6869SZequan Wu A const_ref_use_A(const A &a);
23170b6869SZequan Wu int const_ref_use(const int &i);
24170b6869SZequan Wu A const_use_A(const A a);
25170b6869SZequan Wu int const_use(const int i);
26170b6869SZequan Wu 
f(int a)277096e04aSFangrui Song void f(int a) {
28170b6869SZequan Wu   int i;
29170b6869SZequan Wu   const_ref_use(i);             // expected-warning {{variable 'i' is uninitialized when passed as a const reference argument here}}
30170b6869SZequan Wu   int j = j + const_ref_use(j); // expected-warning {{variable 'j' is uninitialized when used within its own initialization}} expected-warning {{variable 'j' is uninitialized when passed as a const reference argument here}}
31170b6869SZequan Wu   A a1 = const_ref_use_A(a1);   // expected-warning {{variable 'a1' is uninitialized when passed as a const reference argument here}}
32170b6869SZequan Wu   int k = const_use(k);         // expected-warning {{variable 'k' is uninitialized when used within its own initialization}}
33170b6869SZequan Wu   A a2 = const_use_A(a2);       // expected-warning {{variable 'a2' is uninitialized when used within its own initialization}}
34170b6869SZequan Wu   A a3(const_ref_use_A(a3));    // expected-warning {{variable 'a3' is uninitialized when passed as a const reference argument here}}
35170b6869SZequan Wu   A a4 = a3 != a4;              // expected-warning {{variable 'a4' is uninitialized when used within its own initialization}} expected-warning {{variable 'a4' is uninitialized when passed as a const reference argument here}}
36170b6869SZequan Wu   int n = n;                    // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
37170b6869SZequan Wu   const_ref_use(n);
38170b6869SZequan Wu 
39170b6869SZequan Wu   A a5;
40170b6869SZequan Wu   const_ref_use_A(a5);
417096e04aSFangrui Song 
427096e04aSFangrui Song   int m;
437096e04aSFangrui Song   if (a < 42)
447096e04aSFangrui Song     m = 1;
457096e04aSFangrui Song   const_ref_use(m);
46*05470408SZequan Wu 
47*05470408SZequan Wu   int l;
48*05470408SZequan Wu   ignore_template(l); // This is a pattern to avoid "unused variable" warnings (e.g. boost::ignore_unused).
49*05470408SZequan Wu   ignore(l);
50*05470408SZequan Wu   dont_ignore_non_empty(l); // expected-warning {{variable 'l' is uninitialized when passed as a const reference argument here}}
51*05470408SZequan Wu   int l1;
52*05470408SZequan Wu   dont_ignore_block(l1); // expected-warning {{variable 'l1' is uninitialized when passed as a const reference argument here}}
53*05470408SZequan Wu   int l2;
54*05470408SZequan Wu   ignore_function_try_block_maybe_who_knows(l2); // expected-warning {{variable 'l2' is uninitialized when passed as a const reference argument here}}
55170b6869SZequan Wu }
56