Do not trigger -Wmissing-noreturn on lambdas prior to C++23 (#154545)

Fixes #154493

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
This commit is contained in:
Carlos Galvez 2025-08-21 07:30:57 +02:00 committed by GitHub
parent 3a715107c2
commit 3baddbbb0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 1 deletions

View File

@ -209,6 +209,9 @@ Improvements to Clang's diagnostics
potential misaligned members get processed before they can get discarded.
(#GH144729)
- Fixed false positive in ``-Wmissing-noreturn`` diagnostic when it was requiring the usage of
``[[noreturn]]`` on lambdas before C++23 (#GH154493).
Improvements to Clang's time-trace
----------------------------------

View File

@ -1989,6 +1989,11 @@ void clang::inferNoReturnAttr(Sema &S, const Decl *D) {
isKnownToAlwaysThrow(FD)) {
NonConstFD->addAttr(InferredNoReturnAttr::CreateImplicit(S.Context));
// [[noreturn]] can only be added to lambdas since C++23
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD);
MD && !S.getLangOpts().CPlusPlus23 && isLambdaCallOperator(MD))
return;
// Emit a diagnostic suggesting the function being marked [[noreturn]].
S.Diag(FD->getLocation(), diag::warn_suggest_noreturn_function)
<< /*isFunction=*/0 << FD;

View File

@ -1,4 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify %s
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify=expected,cxx17 -std=c++17 %s
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify=expected,cxx23 -std=c++23 %s
namespace std {
class string {
@ -16,6 +17,15 @@ void throwError(const std::string& msg) { // expected-warning {{function 'throwE
throw std::runtime_error(msg);
}
// Using the [[noreturn]] attribute on lambdas is not available until C++23,
// so we should not emit the -Wmissing-noreturn warning on earlier standards.
// Clang supports the attribute on earlier standards as an extension, and emits
// the c++23-lambda-attributes warning.
void lambda() {
auto l1 = [] () { throw std::runtime_error("ERROR"); }; // cxx23-warning {{function 'operator()' could be declared with attribute 'noreturn'}}
auto l2 = [] [[noreturn]] () { throw std::runtime_error("ERROR"); }; // cxx17-warning {{an attribute specifier sequence in this position is a C++23 extension}}
}
// The non-void caller should not warn about missing return.
int ensureZero(int i) {
if (i == 0) return 0;