[clang] Suppress noreturn warning if last statement in a function is a throw (PR #145166)

Yanzuo Liu via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 21 09:17:56 PDT 2025


================
@@ -624,8 +624,32 @@ struct CheckFallThroughDiagnostics {
   }
 };
 
-} // anonymous namespace
+bool isKnownToAlwaysThrow(const FunctionDecl *FD) {
+  if (!FD->hasBody())
+    return false;
+  const Stmt *Body = FD->getBody();
+  const Stmt *OnlyStmt = nullptr;
+
+  if (const auto *Compound = dyn_cast<CompoundStmt>(Body)) {
+    if (Compound->size() != 1)
+      return false; // More than one statement, can't be known to always throw.
+    OnlyStmt = *Compound->body_begin();
+  } else {
+    OnlyStmt = Body;
+  }
+
+  // Unwrap ExprWithCleanups if necessary.
+  if (const auto *EWC = dyn_cast<ExprWithCleanups>(OnlyStmt)) {
+    OnlyStmt = EWC->getSubExpr();
+  }
+  // Check if the only statement is a throw expression.
+  if (isa<CXXThrowExpr>(OnlyStmt)) {
+    return true; // Known to always throw.
+  }
+  return false; // Not known to always throw.
----------------
zwuis wrote:

`return isa<CXXThrowExpr>(OnlyStmt);`

https://github.com/llvm/llvm-project/pull/145166


More information about the cfe-commits mailing list