Author: Carlos Galvez
Date: 2025-08-21T07:30:57+02:00
New Revision: 3baddbbb0a698102073635f7559336f92bc7fe83

URL: 
https://github.com/llvm/llvm-project/commit/3baddbbb0a698102073635f7559336f92bc7fe83
DIFF: 
https://github.com/llvm/llvm-project/commit/3baddbbb0a698102073635f7559336f92bc7fe83.diff

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

Fixes #154493

Co-authored-by: Carlos Gálvez <carlos.gal...@zenseact.com>

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaDeclAttr.cpp
    clang/test/SemaCXX/wmissing-noreturn-suggestion.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d577192d2791f..fe1dd15c6f885 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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
 ----------------------------------
 

diff  --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 625e380f561e2..3685bcefbf764 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