[PATCH] D119094: [clang] Don't emit redundant warnings for 'return;'
Arthur O'Dwyer via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 6 13:47:11 PST 2022
Quuxplusone created this revision.
Quuxplusone added reviewers: rsmith, sammccall, mizvekov, majnemer, riccibruno, clang.
Quuxplusone added a project: clang.
Quuxplusone requested review of this revision.
Herald added a subscriber: cfe-commits.
...when the function declaration's return type is already invalid for
some reason. This is relevant to https://github.com/llvm/llvm-project/issues/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.
(This doesn't actually fix 49188, but it eliminates a surprising redundant warning in the fix I tried, and also eliminates redundant warnings in the test cases depicted here.)
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D119094
Files:
clang/lib/Sema/SemaStmt.cpp
clang/test/SemaCXX/deduced-return-void.cpp
Index: clang/test/SemaCXX/deduced-return-void.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/deduced-return-void.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+
+// Check that we don't get any extra warning for "return" without an
+// expression, in a function that might have been intended to return
+// void all along.
+auto f1() {
+ return 1;
+ return; // expected-error {{deduced as 'void' here but deduced as 'int' in earlier return statement}}
+}
+
+decltype(auto) f2() {
+ return 1;
+ return; // expected-error {{deduced as 'void' here but deduced as 'int' in earlier return statement}}
+}
+
+auto *g() {
+ return; // expected-error {{cannot deduce return type 'auto *' from omitted return expression}}
+}
+
+decltype(h1) h1() { // expected-error {{use of undeclared identifier 'h1'}}
+ return;
+}
Index: clang/lib/Sema/SemaStmt.cpp
===================================================================
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -4098,7 +4098,9 @@
} else if (!RetValExp && !HasDependentReturnType) {
FunctionDecl *FD = getCurFunctionDecl();
- if (getLangOpts().CPlusPlus11 && FD && FD->isConstexpr()) {
+ if (FD->isInvalidDecl()) {
+ // Don't redundantly warn about "return;" if the return type is invalid.
+ } else if (getLangOpts().CPlusPlus11 && FD && FD->isConstexpr()) {
// C++11 [stmt.return]p2
Diag(ReturnLoc, diag::err_constexpr_return_missing_expr)
<< FD << FD->isConsteval();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119094.406278.patch
Type: text/x-patch
Size: 1636 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220206/2b66fc16/attachment.bin>
More information about the cfe-commits
mailing list