xref: /llvm-project/clang/test/SemaCXX/return.cpp (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 %s -std=c++11 -fcxx-exceptions -fexceptions -fsyntax-only -Wignored-qualifiers -verify
2 
test1()3 int test1() {
4   throw;
5 }
6 
7 // PR5071
f()8 template<typename T> T f() { }
9 
10 template<typename T>
g(T t)11 void g(T t) {
12   return t * 2; // okay
13 }
14 
15 template<typename T>
h()16 T h() {
17   return 17;
18 }
19 
20 // Don't warn on cv-qualified class return types, only scalar return types.
21 namespace ignored_quals {
22 struct S {};
23 const S class_c();
24 const volatile S class_cv();
25 
26 const int scalar_c(); // expected-warning{{'const' type qualifier on return type has no effect}}
27 int const scalar_c2(); // expected-warning{{'const' type qualifier on return type has no effect}}
28 
29 const
30 char*
31 const // expected-warning{{'const' type qualifier on return type has no effect}}
32 f();
33 
34 char
35 const*
36 const // expected-warning{{'const' type qualifier on return type has no effect}}
37 g();
38 
39 char* const h(); // expected-warning{{'const' type qualifier on return type has no effect}}
40 char* volatile i(); // expected-warning{{'volatile' type qualifier on return type has no effect}}
41 
42 char*
43 volatile // expected-warning{{'const volatile' type qualifiers on return type have no effect}}
44 const
45 j();
46 
47 const volatile int scalar_cv(); // expected-warning{{'const volatile' type qualifiers on return type have no effect}}
48 
49 // FIXME: Maintain enough information that we can point the diagnostic at the 'volatile' keyword.
50 const
51 int S::*
52 volatile
53 mixed_ret(); // expected-warning {{'volatile' type qualifier on return type has no effect}}
54 
55 const int volatile // expected-warning {{'const volatile' type qualifiers on return type have no effect}}
56     (((parens())));
57 
58 _Atomic(int) atomic();
59 
60 _Atomic // expected-warning {{'_Atomic' type qualifier on return type has no effect}}
61     int
62     atomic();
63 
64 auto trailing_return_type() ->
65     const int; // expected-warning {{'const' type qualifier on return type has no effect}}
66 
67 auto trailing_return_type_lambda = [](const int &x) ->
68     const int // expected-warning {{'const' type qualifier on return type has no effect}}
__anoncfefb17e0202(const int &x) 69     { return x; };
70 
71 const int ret_array()[4]; // expected-error {{cannot return array}}
72 }
73 
74 namespace PR9328 {
75   typedef char *PCHAR;
76   class Test
77   {
GetName()78     const PCHAR GetName() { return 0; } // expected-warning{{'const' type qualifier on return type has no effect}}
79   };
80 }
81 
82 class foo  {
83   operator const int ();
84   operator int * const ();
85 };
86 
87 namespace PR10057 {
88   struct S {
89     ~S();
90   };
91 
92   template <class VarType>
Test(const VarType & value)93   void Test(const VarType& value) {
94     return S() = value;
95   }
96 }
97 
98 namespace return_has_expr {
99   struct S {
Sreturn_has_expr::S100     S() {
101       return 42; // expected-error {{constructor 'S' should not return a value}}
102     }
~Sreturn_has_expr::S103     ~S() {
104       return 42; // expected-error {{destructor '~S' should not return a value}}
105     }
106   };
107 }
108 
109 // pr17759
110 namespace ctor_returns_void {
f()111   void f() {}
112   struct S {
Sctor_returns_void::S113     S() { return f(); } // expected-error {{constructor 'S' must not return void expression}}
~Sctor_returns_void::S114     ~S() { return f(); } // expected-error {{destructor '~S' must not return void expression}}
115   };
116 
117   template <typename T> struct ST {
STctor_returns_void::ST118     ST() { return f(); } // expected-error {{constructor 'ST<T>' must not return void expression}}
119                          // expected-error@-1 {{constructor 'ST' must not return void expression}}
~STctor_returns_void::ST120     ~ST() { return f(); } // expected-error {{destructor '~ST<T>' must not return void expression}}
121                           // expected-error@-1 {{destructor '~ST' must not return void expression}}
122   };
123 
124   ST<int> st; // expected-note {{in instantiation of member function 'ctor_returns_void::ST<int>::ST'}}
125               // expected-note@-1 {{in instantiation of member function 'ctor_returns_void::ST<int>::~ST'}}
126 }
127 
cxx_unresolved_expr()128 void cxx_unresolved_expr() {
129   // The use of an undeclared variable tricks clang into building a
130   // CXXUnresolvedConstructExpr, and the missing ')' gives it an invalid source
131   // location for its rparen.  Check that emitting a diag on the range of the
132   // expr doesn't assert.
133   return int(undeclared, 4; // expected-error {{expected ')'}} expected-note{{to match this '('}} expected-error {{use of undeclared identifier 'undeclared'}}
134 }
135