Skip to content

Commit 3c8d2aa

Browse files
committed
[clang] Don't emit redundant warnings for 'return;'
when the function declaration's return type is already invalid for some reason. This is relevant to llvm#49188 because another way that the declaration's return type could become invalid is that it might be `C auto` where `C<void>` is false. Differential Revision: https://reviews.llvm.org/D119094
1 parent 528deed commit 3c8d2aa

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

clang/lib/Sema/SemaStmt.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4098,7 +4098,9 @@ StmtResult Sema::BuildReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp,
40984098
} else if (!RetValExp && !HasDependentReturnType) {
40994099
FunctionDecl *FD = getCurFunctionDecl();
41004100

4101-
if (getLangOpts().CPlusPlus11 && FD && FD->isConstexpr()) {
4101+
if ((FD && FD->isInvalidDecl()) || FnRetType->containsErrors()) {
4102+
// The intended return type might have been "void", so don't warn.
4103+
} else if (getLangOpts().CPlusPlus11 && FD && FD->isConstexpr()) {
41024104
// C++11 [stmt.return]p2
41034105
Diag(ReturnLoc, diag::err_constexpr_return_missing_expr)
41044106
<< FD << FD->isConsteval();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.
7+
auto f1() {
8+
return 1;
9+
return; // expected-error {{deduced as 'void' here but deduced as 'int' in earlier return statement}}
10+
}
11+
12+
decltype(auto) f2() {
13+
return 1;
14+
return; // expected-error {{deduced as 'void' here but deduced as 'int' in earlier return statement}}
15+
}
16+
17+
auto *g() {
18+
return; // expected-error {{cannot deduce return type 'auto *' from omitted return expression}}
19+
}
20+
21+
decltype(h1) h1() { // expected-error {{use of undeclared identifier 'h1'}}
22+
return;
23+
}

0 commit comments

Comments
 (0)