Skip to content

Commit 3baddbb

Browse files
carlosgalvezpCarlos Gálvez
andauthored
Do not trigger -Wmissing-noreturn on lambdas prior to C++23 (llvm#154545)
Fixes llvm#154493 Co-authored-by: Carlos Gálvez <[email protected]>
1 parent 3a71510 commit 3baddbb

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ Improvements to Clang's diagnostics
209209
potential misaligned members get processed before they can get discarded.
210210
(#GH144729)
211211

212+
- Fixed false positive in ``-Wmissing-noreturn`` diagnostic when it was requiring the usage of
213+
``[[noreturn]]`` on lambdas before C++23 (#GH154493).
214+
212215
Improvements to Clang's time-trace
213216
----------------------------------
214217

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,11 @@ void clang::inferNoReturnAttr(Sema &S, const Decl *D) {
19891989
isKnownToAlwaysThrow(FD)) {
19901990
NonConstFD->addAttr(InferredNoReturnAttr::CreateImplicit(S.Context));
19911991

1992+
// [[noreturn]] can only be added to lambdas since C++23
1993+
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD);
1994+
MD && !S.getLangOpts().CPlusPlus23 && isLambdaCallOperator(MD))
1995+
return;
1996+
19921997
// Emit a diagnostic suggesting the function being marked [[noreturn]].
19931998
S.Diag(FD->getLocation(), diag::warn_suggest_noreturn_function)
19941999
<< /*isFunction=*/0 << FD;

clang/test/SemaCXX/wmissing-noreturn-suggestion.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify %s
1+
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify=expected,cxx17 -std=c++17 %s
2+
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify=expected,cxx23 -std=c++23 %s
23

34
namespace std {
45
class string {
@@ -16,6 +17,15 @@ void throwError(const std::string& msg) { // expected-warning {{function 'throwE
1617
throw std::runtime_error(msg);
1718
}
1819

20+
// Using the [[noreturn]] attribute on lambdas is not available until C++23,
21+
// so we should not emit the -Wmissing-noreturn warning on earlier standards.
22+
// Clang supports the attribute on earlier standards as an extension, and emits
23+
// the c++23-lambda-attributes warning.
24+
void lambda() {
25+
auto l1 = [] () { throw std::runtime_error("ERROR"); }; // cxx23-warning {{function 'operator()' could be declared with attribute 'noreturn'}}
26+
auto l2 = [] [[noreturn]] () { throw std::runtime_error("ERROR"); }; // cxx17-warning {{an attribute specifier sequence in this position is a C++23 extension}}
27+
}
28+
1929
// The non-void caller should not warn about missing return.
2030
int ensureZero(int i) {
2131
if (i == 0) return 0;

0 commit comments

Comments
 (0)