xref: /llvm-project/clang/test/SemaCXX/deduced-return-void.cpp (revision 989f76ce9064caf08200a214ff634d8eb6fa494b)
1 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
3 
4 // Check that we don't get any extra warning for "return" without an
5 // expression, in a function that might have been intended to return
6 // void all along.
h1()7 decltype(h1) h1() { // expected-error {{use of undeclared identifier 'h1'}}
8   return;
9 }
10 
11 namespace JustAuto {
12 int i;
f1()13 auto f1() { }
f2()14 auto f2() { return; }
f3()15 auto f3() { return void(); }
f4()16 auto f4() {
17   return i;
18   return; // expected-error {{'auto' in return type deduced as 'void' here but deduced as 'int' in earlier return statement}}
19 }
f5()20 auto f5() {
21   return i;
22   return void(); // expected-error {{'auto' in return type deduced as 'void' here but deduced as 'int' in earlier return statement}}
23 }
24 
__anon22edc4580102() 25 auto l1 = []() { };
__anon22edc4580202() 26 auto l2 = []() { return; };
__anon22edc4580302() 27 auto l3 = []() { return void(); };
__anon22edc4580402() 28 auto l4 = []() {
29   return i;
30   return; // expected-error {{return type 'void' must match previous return type 'int' when lambda expression has unspecified explicit return type}}
31 };
__anon22edc4580502() 32 auto l5 = []() {
33   return i;
34   return void(); // expected-error {{return type 'void' must match previous return type 'int' when lambda expression has unspecified explicit return type}}
35 };
36 
37 } // namespace JustAuto
38 
39 namespace DecltypeAuto {
40 int i;
f1()41 decltype(auto) f1() { }
f2()42 decltype(auto) f2() { return; }
f3()43 decltype(auto) f3() { return void(); }
f4()44 decltype(auto) f4() {
45   return i;
46   return; // expected-error {{'decltype(auto)' in return type deduced as 'void' here but deduced as 'int' in earlier return statement}}
47 }
f5()48 decltype(auto) f5() {
49   return i;
50   return void(); // expected-error {{'decltype(auto)' in return type deduced as 'void' here but deduced as 'int' in earlier return statement}}
51 }
52 
53 auto l1 = []() -> decltype(auto) { };
54 auto l2 = []() -> decltype(auto) { return; };
55 auto l3 = []() -> decltype(auto) { return void(); };
56 auto l4 = []() -> decltype(auto) {
57   return i;
58   return; // expected-error {{'decltype(auto)' in return type deduced as 'void' here but deduced as 'int' in earlier return statement}}
59 };
60 auto l5 = []() -> decltype(auto) {
61   return i;
62   return void(); // expected-error {{'decltype(auto)' in return type deduced as 'void' here but deduced as 'int' in earlier return statement}}
63 };
64 
65 } // namespace DecltypeAuto
66 
67 namespace AutoPtr {
68 int i;
f1()69 auto *f1() { } // expected-error {{cannot deduce return type 'auto *' for function with no return statements}}
f2()70 auto *f2() {
71   return; // expected-error {{cannot deduce return type 'auto *' from omitted return expression}}
72 }
f3()73 auto *f3() {
74   return void(); // expected-error {{cannot deduce return type 'auto *' from returned value of type 'void'}}
75 }
f4()76 auto *f4() {
77   return &i;
78   return; // expected-error {{cannot deduce return type 'auto *' from omitted return expression}}
79 }
f5()80 auto *f5() {
81   return &i;
82   return void(); // expected-error {{cannot deduce return type 'auto *' from returned value of type 'void'}}
83 }
84 
__anon22edc4580602() 85 auto l1 = []() -> auto* { }; // expected-error {{cannot deduce return type 'auto *' for function with no return statements}}
__anon22edc4580702() 86 auto l2 = []() -> auto* {
87   return; // expected-error {{cannot deduce return type 'auto *' from omitted return expression}}
88 };
__anon22edc4580802() 89 auto l3 = []() -> auto* {
90   return void(); // expected-error {{cannot deduce return type 'auto *' from returned value of type 'void'}}
91 };
__anon22edc4580902() 92 auto l4 = []() -> auto* {
93   return &i;
94   return; // expected-error {{cannot deduce return type 'auto *' from omitted return expression}}
95 };
__anon22edc4580a02() 96 auto l5 = []() -> auto* {
97   return &i;
98   return void(); // expected-error {{cannot deduce return type 'auto *' from returned value of type 'void'}}
99 };
100 } // namespace AutoPtr
101 
102 namespace AutoRef {
103 int i;
f1()104 auto& f1() { // expected-error {{cannot deduce return type 'auto &' for function with no return statements}}
105 }
f2()106 auto& f2() {
107   return; // expected-error {{cannot deduce return type 'auto &' from omitted return expression}}
108 }
f3()109 auto& f3() {
110   return void(); // expected-error@-1 {{cannot form a reference to 'void'}}
111 }
f4()112 auto& f4() {
113   return i;
114   return; // expected-error {{cannot deduce return type 'auto &' from omitted return expression}}
115 }
f5()116 auto& f5() {
117   return i;
118   return void(); // expected-error {{deduced as 'int' in earlier return statement}}
119 }
f6()120 auto& f6() { return 42; } // expected-error {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
121 
__anon22edc4580b02() 122 auto l1 = []() -> auto& { }; // expected-error {{cannot deduce return type 'auto &' for function with no return statements}}
__anon22edc4580c02() 123 auto l2 = []() -> auto& {
124   return; // expected-error {{cannot deduce return type 'auto &' from omitted return expression}}
125 };
__anon22edc4580d02() 126 auto l3 = []() -> auto& { // expected-error {{cannot form a reference to 'void'}}
127   return void();
128 };
__anon22edc4580e02() 129 auto l4 = []() -> auto& {
130   return i;
131   return; // expected-error {{cannot deduce return type 'auto &' from omitted return expression}}
132 };
__anon22edc4580f02() 133 auto l5 = []() -> auto & {
134   return i;
135   return void(); // expected-error {{deduced as 'int' in earlier return statement}}
136 };
__anon22edc4581002() 137 auto l6 = []() -> auto& {
138   return 42; // expected-error {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
139 };
140 } // namespace AutoRef
141