Skip to content

Commit 529e5af

Browse files
committed
[clang-tidy] ignore consteval function in ExceptionAnalyzer
`ExceptionAnalyzer` can ignore `consteval` function even if it will throw exception. `consteval` function must produce compile-time constant. But throw statement cannot appear in constant evaluation. Fixed: #104457.
1 parent 3f9d02a commit 529e5af

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ bool isQualificationConvertiblePointer(QualType From, QualType To,
320320
} // namespace
321321

322322
static bool canThrow(const FunctionDecl *Func) {
323+
// consteval specifies every call to the function must produce a compile-time
324+
// constant. compile-time constant cannot be evaluate a throw expression.
325+
if (Func->isConsteval())
326+
return false;
327+
323328
const auto *FunProto = Func->getType()->getAs<FunctionProtoType>();
324329
if (!FunProto)
325330
return true;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ Changes in existing checks
162162
<clang-tidy/checks/bugprone/dangling-handle>` check to treat `std::span` as a
163163
handle class.
164164

165+
- Improved :doc:`bugprone-exception-escape
166+
<clang-tidy/checks/bugprone/exception-escape>` by fixing false positives
167+
when consteval function with throw statements.
168+
165169
- Improved :doc:`bugprone-forwarding-reference-overload
166170
<clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing
167171
a crash when determining if an ``enable_if[_t]`` was found.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %check_clang_tidy -std=c++20 %s bugprone-exception-escape %t -- \
2+
// RUN: -- -fexceptions -Wno-everything
3+
4+
namespace GH104457 {
5+
6+
consteval int consteval_fn(int a) {
7+
if (a == 0)
8+
throw 1;
9+
return a;
10+
}
11+
12+
int test() noexcept { return consteval_fn(1); }
13+
14+
} // namespace GH104457

0 commit comments

Comments
 (0)