https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/154545

>From 0e006a3e28d4d3a6a0faeb88caab344e08fcde38 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= <carlos.gal...@zenseact.com>
Date: Wed, 20 Aug 2025 13:46:58 +0000
Subject: [PATCH] Do not trigger -Wmissing-noreturn on lambdas prior to C++23

Fixes #154493
---
 clang/docs/ReleaseNotes.rst                         |  3 +++
 clang/lib/Sema/SemaDeclAttr.cpp                     |  5 +++++
 clang/test/SemaCXX/wmissing-noreturn-suggestion.cpp | 12 +++++++++++-
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5c780851ca589..5a837642ca99b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -188,6 +188,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
 ----------------------------------
 
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 7a185106e4c6e..8c3130a7d4735 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -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;
diff --git a/clang/test/SemaCXX/wmissing-noreturn-suggestion.cpp 
b/clang/test/SemaCXX/wmissing-noreturn-suggestion.cpp
index 8beffcd39e85c..06db972118d1c 100644
--- a/clang/test/SemaCXX/wmissing-noreturn-suggestion.cpp
+++ b/clang/test/SemaCXX/wmissing-noreturn-suggestion.cpp
@@ -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;

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to