xref: /llvm-project/clang/test/SemaCXX/warn-uninitialized-const-reference.cpp (revision 054704082b461418d3dac3a379792cdaf52d40b3)
1 // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wuninitialized-const-reference -verify %s
2 
3 class A {
4 public:
5   int i;
A()6   A(){};
A(const A & a)7   A(const A &a){};
A(int i)8   A(int i) {}
9   bool operator!=(const A &);
10 };
11 
12 template <class T>
ignore_template(const T &)13 void ignore_template(const T &) {}
ignore(const int & i)14 void ignore(const int &i) {}
dont_ignore_non_empty(const int & i)15 void dont_ignore_non_empty(const int &i) { ; } // Calling this won't silence the warning for you
dont_ignore_block(const int & i)16 void dont_ignore_block(const int &i) {
17   {}
18 } // Calling this won't silence the warning for you
ignore_function_try_block_maybe_who_knows(const int &)19 void ignore_function_try_block_maybe_who_knows(const int &) try {
20 } catch (...) {
21 }
22 A const_ref_use_A(const A &a);
23 int const_ref_use(const int &i);
24 A const_use_A(const A a);
25 int const_use(const int i);
26 
f(int a)27 void f(int a) {
28   int i;
29   const_ref_use(i);             // expected-warning {{variable 'i' is uninitialized when passed as a const reference argument here}}
30   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}}
31   A a1 = const_ref_use_A(a1);   // expected-warning {{variable 'a1' is uninitialized when passed as a const reference argument here}}
32   int k = const_use(k);         // expected-warning {{variable 'k' is uninitialized when used within its own initialization}}
33   A a2 = const_use_A(a2);       // expected-warning {{variable 'a2' is uninitialized when used within its own initialization}}
34   A a3(const_ref_use_A(a3));    // expected-warning {{variable 'a3' is uninitialized when passed as a const reference argument here}}
35   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}}
36   int n = n;                    // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
37   const_ref_use(n);
38 
39   A a5;
40   const_ref_use_A(a5);
41 
42   int m;
43   if (a < 42)
44     m = 1;
45   const_ref_use(m);
46 
47   int l;
48   ignore_template(l); // This is a pattern to avoid "unused variable" warnings (e.g. boost::ignore_unused).
49   ignore(l);
50   dont_ignore_non_empty(l); // expected-warning {{variable 'l' is uninitialized when passed as a const reference argument here}}
51   int l1;
52   dont_ignore_block(l1); // expected-warning {{variable 'l1' is uninitialized when passed as a const reference argument here}}
53   int l2;
54   ignore_function_try_block_maybe_who_knows(l2); // expected-warning {{variable 'l2' is uninitialized when passed as a const reference argument here}}
55 }
56