[clang] Fix an issue where we erroneously diagnose a `[[noreturn]]` function with `warn_suggest_noreturn_function` (PR #148695)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 14 11:15:44 PDT 2025
https://github.com/term-est created https://github.com/llvm/llvm-project/pull/148695
The following snippet,
```c++
#include <cassert>
#include <stdexcept>
[[noreturn]] void foo() { throw std::runtime_error("Why...?"); }
int main() {
try {
foo();
} catch (std::runtime_error const& ex) {
(void)ex;
}
}
```
is diagnosed with `error: function 'foo' could be declared with attribute 'noreturn' [-Werror,-Wmissing-noreturn]` when compiled with clang_trunk using `-Werror=missing-noreturn` flag. See more at [godbolt](https://godbolt.org/z/5zMsev4as)
AST shows
```
| `-CXX11NoReturnAttr 0x1896e81eb00 <line:2:20> Inherited noreturn
|-FunctionDecl 0x1896e81ebd8 <line:10:14, col:46> col:19 used foo 'void ()'
| |-CompoundStmt 0x1896e81ee20 <col:25, col:46>
| | `-CXXThrowExpr 0x1896e81ee08 <col:27, col:43> 'void'
| | `-ImplicitCastExpr 0x1896e81edf0 <col:33, col:43> 'const char *' <ArrayToPointerDecay>
| | `-ParenExpr 0x1896e81eda0 <col:33, col:43> 'const char[8]' lvalue
| | `-StringLiteral 0x1896e81ed80 <col:34> 'const char[8]' lvalue "Why...?"
| |-CXX11NoReturnAttr 0x1896e81ec80 <col:3> noreturn
| `-InferredNoReturnAttr 0x1896e81ee38 <<invalid sloc>> Implicit
```
but the responsible code only checks for `NoReturnAttr`, adding a check for `CXX11NoReturnAttr` before emitting the diagnostic should fix this issue.
>From f8082dde0f087436df3c6e8e2f36099893f1dae4 Mon Sep 17 00:00:00 2001
From: term-est <eurasia.sely at gmail.com>
Date: Mon, 14 Jul 2025 21:07:58 +0300
Subject: [PATCH] Fix an issue where we erroneously diagnose a `[[noreturn]]`
function with `warn_suggest_noreturn_function`
---
clang/lib/Sema/SemaDeclAttr.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 099207727c8c8..6d0a5d71d49ff 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -1976,7 +1976,7 @@ void clang::inferNoReturnAttr(Sema &S, const Decl *D) {
Diags.isIgnored(diag::warn_suggest_noreturn_function, FD->getLocation()))
return;
- if (!FD->hasAttr<NoReturnAttr>() && !FD->hasAttr<InferredNoReturnAttr>() &&
+ if (!FD->hasAttr<NoReturnAttr>() && !FD->hasAttr<CXX11NoReturnAttr>() && !FD->hasAttr<InferredNoReturnAttr>() &&
isKnownToAlwaysThrow(FD)) {
NonConstFD->addAttr(InferredNoReturnAttr::CreateImplicit(S.Context));
More information about the cfe-commits
mailing list